我有兩個字典座標:建立從兩個字典中的一個被比較的密鑰的新詞典,Python的
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左側的鍵在這個過程結束時,它們都會以結果字典結束,即dict2的值集合等於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' – Pukki