我有两个带有坐标的字典:
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]}
这可能很重要,也可能不重要,但我的数据结构是这样的,这个过程中字典2中没有键会留下来,它们都将最终进入结果字典,即字典2的值集合等于字典1的值集合。
我实现的方式是:
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
是从哪里来的? - Bob Dylan