我想將字典存儲在一個主字典中,作爲存儲變量歷史的一種方式,以便稍後可以訪問它們(如果需要的話)。當添加另一個詞典作爲值時字典不能正確更新
我定義詞典作爲這樣:
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中的表格名稱等...
那麼,爲什麼在字典中這樣做會導致它失敗,當用元組進行工作時效果很好?
謝謝,這正是我需要的。我一直在困惑這一個多小時,我知道它必須是這樣簡單的事情。 – 1saac