我想将字典存储在一个主字典中,作为存储变量历史的一种方式,以便稍后可以访问它们(如果需要的话)。当添加另一个词典作为值时字典不能正确更新
我定义词典作为这样:
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