2017-05-09 62 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