2017-05-09 60 views
0

我想將字典存儲在一個主字典中,作爲存儲變量歷史的一種方式,以便稍後可以訪問它們(如果需要的話)。當添加另一個詞典作爲值時字典不能正確更新

我定義詞典作爲這樣:

self.current = {} 
self.version = {} 

self.current將存儲兩個密鑰/值對將nodes映射的話到電流節點SQL表和單詞edges到電流邊緣SQL表。

作爲通過程序重複,它使新的表來表示在系統中的邊緣和節點,所以我想本身存儲self.current作爲值在self.version,使得time變量可以訪問的狀態系統在任何以前的時間步驟。

例如,如果有10次迭代總共超過self.current['nodes']應該返回'dhn_vertices_pgr9'self.current['edges']應該返回'dhn9'

我這樣做的:

self.version[time] = self.current 

在每次迭代結束。 Howevever,每一次的self.version表中有映射到每一個時間步最近詞典:

使用打印語句:

print('iteration {0}, current nodes = {1}'.format(time, self.current['nodes'])) 
print('iteration {0}, current edges = {1}'.format(time, self.current['edges'])) 
print('iteration {0}, time = {1}'.format(time, time)) 
print('iteration {0}, self.current = {1}'.format(time, self.current)) 
self.version[time] = self.current 
print('iteration {0}, self.version = {1}\n'.format(time, self.version)) 

顯示控制檯:

iteration 0, current nodes = dhn_0_vertices_pgr 
iteration 0, current edges = dhn_0 
iteration 0, time = 0 
iteration 0, self.current = {'edges': 'dhn_0', 'nodes': 'dhn_0_vertices_pgr'} 
iteration 0, self.version = {0: {'edges': 'dhn_0', 'nodes': 'dhn_0_vertices_pgr'}} 

iteration 1, current nodes = dhn_1_vertices_pgr 
iteration 1, current edges = dhn_1 
iteration 1, time = 1 
iteration 1, self.current = {'edges': 'dhn_1', 'nodes': 'dhn_1_vertices_pgr'} 
iteration 1, self.version = {0: {'edges': 'dhn_1', 'nodes': 'dhn_1_vertices_pgr'}, 1: {'edges': 'dhn_1', 'nodes': 'dhn_1_vertices_pgr'}} 

iteration 2, current nodes = dhn_2_vertices_pgr 
iteration 2, current edges = dhn_2 
iteration 2, time = 2 
iteration 2, self.current = {'edges': 'dhn_2', 'nodes': 'dhn_2_vertices_pgr'} 
iteration 2, self.version = {0: {'edges': 'dhn_2', 'nodes': 'dhn_2_vertices_pgr'}, 1: {'edges': 'dhn_2', 'nodes': 'dhn_2_vertices_pgr'}, 2: {'edges': 'dhn_2', 'nodes': 'dhn_2_vertices_pgr'}} 

iteration 3, current nodes = dhn_3_vertices_pgr 
iteration 3, current edges = dhn_3 
iteration 3, time = 3 
iteration 3, self.current = {'edges': 'dhn_3', 'nodes': 'dhn_3_vertices_pgr'} 
iteration 3, self.version = {0: {'edges': 'dhn_3', 'nodes': 'dhn_3_vertices_pgr'}, 1: {'edges': 'dhn_3', 'nodes': 'dhn_3_vertices_pgr'}, 2: {'edges': 'dhn_3', 'nodes': 'dhn_3_vertices_pgr'}, 3: {'edges': 'dhn_3', 'nodes': 'dhn_3_vertices_pgr'}} 

正如你可以看到,self.version中的每個值都是最新的條目。

但是沒有意義對我來說,如果我只是兩個條目保存爲一個元組像這樣:用打印語句

self.version[time] = (self.current['edges'], self.current['nodes']) 

並再次:

print('iteration {0}, current nodes = {1}'.format(time, self.current['nodes'])) 
print('iteration {0}, current edges = {1}'.format(time, self.current['edges'])) 
print('iteration {0}, time = {1}'.format(time, time)) 
print('iteration {0}, self.current = {1}'.format(time, self.current)) 
self.version[time] = (self.current['edges'], self.current['nodes']) 
print('iteration {0}, self.version = {1}\n'.format(time, self.version)) 

給人的控制檯輸出:

iteration 0, current nodes = dhn_0_vertices_pgr 
iteration 0, current edges = dhn_0 
iteration 0, time = 0 
iteration 0, self.current = {'edges': 'dhn_0', 'nodes': 'dhn_0_vertices_pgr'} 
iteration 0, self.version = {0: ('dhn_0', 'dhn_0_vertices_pgr')} 

iteration 1, current nodes = dhn_1_vertices_pgr 
iteration 1, current edges = dhn_1 
iteration 1, time = 1 
iteration 1, self.current = {'edges': 'dhn_1', 'nodes': 'dhn_1_vertices_pgr'} 
iteration 1, self.version = {0: ('dhn_0', 'dhn_0_vertices_pgr'), 1: ('dhn_1', 'dhn_1_vertices_pgr')} 

iteration 2, current nodes = dhn_2_vertices_pgr 
iteration 2, current edges = dhn_2 
iteration 2, time = 2 
iteration 2, self.current = {'edges': 'dhn_2', 'nodes': 'dhn_2_vertices_pgr'} 
iteration 2, self.version = {0: ('dhn_0', 'dhn_0_vertices_pgr'), 1: ('dhn_1', 'dhn_1_vertices_pgr'), 2: ('dhn_2', 'dhn_2_vertices_pgr')} 

iteration 3, current nodes = dhn_3_vertices_pgr 
iteration 3, current edges = dhn_3 
iteration 3, time = 3 
iteration 3, self.current = {'edges': 'dhn_3', 'nodes': 'dhn_3_vertices_pgr'} 
iteration 3, self.version = {0: ('dhn_0', 'dhn_0_vertices_pgr'), 1: ('dhn_1', 'dhn_1_vertices_pgr'), 2: ('dhn_2', 'dhn_2_vertices_pgr'), 3: ('dhn_3', 'dhn_3_vertices_pgr')} 

正如您所看到的,時間步1現在實際上映射到表名,因爲它們是我n時間步驟1,時間步驟2映射到時間步驟2中的表格名稱,時間步驟3中的表格名稱等...

那麼,爲什麼在字典中這樣做會導致它失敗,當用元組進行工作時效果很好?

回答

2

當分配

self.version[time] = (self.current) 

你不指定字典的價值,但相同的字典的參考。請看下面的例子:

dictA = {} 
dictB = {} 
dictA = dictB 
dictB['foo'] = 'bar' 
print(dictA) //{'foo': 'bar'} 
print(dictB) //{'foo': 'bar'} 

在這個例子中,當你將dictA = dictB你點都dictAdictB同一個對象。所以改變dictB也將改變dictA(和相反)爲了解決這個問題,你需要複製的值dictB(而不是參考)。而你根據dict(dictB)做到這一點:

dictA = {} 
dictB = {} 
dictA = dict(dictB) //the value of dictB is now copied, not the reference 
dictB['foo'] = 'bar' 
print(dictA) //{} 
print(dictB) //{'foo': 'bar'} 

回到你的例子:更改self.current也會影響self.version。因此,要解決這個變化:

self.version[time] = dict(self.current) 

How to copy a dictionary and only edit the copy

+0

謝謝,這正是我需要的。我一直在困惑這一個多小時,我知道它必須是這樣簡單的事情。 – 1saac