【发布时间】:2015-08-24 18:10:42
【问题描述】:
我有两个带坐标的字典:
vertex_coordinates = {0: [x0,y0,z0], 1: [x1,y1,z1], 2: [x2,y2,z2] ...}
element_coordinates = {0: [X0,Y0,Z0], 2: [X2,Y2,Z2], 7: [X3,Y3,Z3] ...}
第一个字典的键是简单的 0:N,而第二个字典的键是排序的,但不一定是连续的。第二个字典实际上比第一个大得多,所以一个特殊情况是
len(vertex_coordinates) = 729
len(element_coordinates) = 58752
我想要的是一个字典,其中键表示第一个字典的键,与此键关联的值是第二个字典中的键列表,这样坐标相等。 例如,让
vertex_coordinates = {0: [1.0,1.0,1.0], 1: [0.0,0.0,0.0], 2: [3.0,4.0,5.0], 3: [3.0, 6.0, 7.0]}
element_coordinates = {0: [0.0,0.0,0.0], 1: [3.0,4.0,5.0], 3: [3.0,6.0,7.0], \
4: [1.0,1.0,1.0], 6: [0.0,0.0,0.0], 7: [3.0,4.0,5.0], 8:[1.0,1.0,1.0] \
10: [3.0,6.0,7.0]}
那么,想要的字典是
element_to_vertex = {0: [4,8], 1: [0,6], 2: [1,7], 3: [3,10]}
这可能很重要,也可能不重要,但我的数据结构是这样的等于dict1的设定值。
我的实现方式是:
for vertex in vertex_coordinates:
temp = []
for elem in element_coordinates:
if(near(element_coordinates[elem][0], vertex_coordinates[vertex][0])):
if(near(element_coordinates[elem][1], vertex_coordinates[vertex][1])):
if(near(element_coordinates[elem][2], vertex_coordinates[vertex][2])):
temp.append(elem)
element_to_vertex[vertex] = temp
虽然这工作正常,但速度很慢:在字典长度为 729 和 58752 的示例中,运行大约需要 25 秒,而这些长度并不是我感兴趣的最大长度。您能否告诉我是否可以加快速度,或者我是否应该考虑另一种解决此问题的方法? 谢谢。
【问题讨论】:
-
dof来自哪里? -
我的错,编辑它。它应该是'elem'
标签: python dictionary