这是我在另一个字典中添加字典的代码
dict_temp = {}
logs = {}
for d in e:
count +=1
dict_temp['email']= d["sent_to"]
dict_temp['log_id']= d["unique_arguments"]
print dict_temp
logs[count] = dict_temp
dict_temp.clear()
print logs
字典dict_temp是完美构建的,但是当我将这个字典添加到日志中时,dictonar在我打印时查看字典,它是空的.. 这是输出;
{'log_id': u'log_1_taqi.official@gmail.com', 'email': u'taqi.official@gmail.com'}
{'log_id': u'log_1_taqi.hass@cogilent.com', 'email': u'taqi.hass@cogilent.com'}
{'log_id': u'log_1_taqi.official@gmailllllll.com', 'email': u'taqi.official@gmailllllll.com'}
{'log_id': u'log_2_taqi.official@gmail.com', 'email': u'taqi.official@gmail.com'}
{'log_id': u'log_2_taqi.hass@cogilent.com', 'email': u'taqi.hass@cogilent.com'}
{'log_id': u'log_2_taqi.official@gmailllllll.com', 'email': u'taqi.official@gmailllllll.com'}
{'log_id': u'log_4_taqi.official@gmail.com', 'email': u'taqi.official@gmail.com'}
{'log_id': u'log_4_taqi.hass@cogilent.com', 'email': u'taqi.hass@cogilent.com'}
{'log_id': u'log_4_taqi.official@gmailllllll.com', 'email': u'taqi.official@gmailllllll.com'}
{'log_id': u'log_5_taqi.official@gmail.com', 'email': u'taqi.official@gmail.com'}
{'log_id': u'log_5_taqi.hass@cogilent.com', 'email': u'taqi.hass@cogilent.com'}
{'log_id': u'log_5_taqi.official@gmailllllll.com', 'email': u'taqi.official@gmailllllll.com'}
{'log_id': u'log_6_taqi.official@gmail.com', 'email': u'taqi.official@gmail.com'}
{'log_id': u'log_6_taqi.hass@cogilent.com', 'email': u'taqi.hass@cogilent.com'}
{'log_id': u'log_6_taqi.official@gmailllllll.com', 'email': u'taqi.official@gmailllllll.com'}
{'log_id': u'log_7_taqi.official@gmail.com', 'email': u'taqi.official@gmail.com'}
{'log_id': u'log_7_taqi.hass@cogilent.com', 'email': u'taqi.hass@cogilent.com'}
{'log_id': u'log_7_taqi.official@gmailllllll.com', 'email': u'taqi.official@gmailllllll.com'}
{'log_id': u'log_8_taqi.official@gmail.com', 'email': u'taqi.official@gmail.com'}
{'log_id': u'log_8_taqi.hass@cogilent.com', 'email': u'taqi.hass@cogilent.com'}
{'log_id': u'log_8_taqi.official@gmailllllll.com', 'email': u'taqi.official@gmailllllll.com'}
{1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {}, 8: {}, 9: {}, 10: {}, 11: {}, 12: {}, 13: {}, 14: {}, 15: {}, 16: {}, 17: {}, 18: {}, 19: {}, 20: {}, 21: {}}
答案 0 :(得分:3)
您反复添加相同字典对象。您没有添加副本。因此,您最终将对该字典的所有引用显示相同的内容:
>>> inner = {}
>>> outer = {}
>>> inner['foo'] = 'bar'
>>> outer['inner_key'] = inner
>>> outer
{'inner_key': {'foo': 'bar'}}
>>> inner['foo'] = 'baz'
>>> outer
{'inner_key': {'foo': 'baz'}}
>>> inner.clear()
>>> outer
{'inner_key': {}}
>>> outer['another_key'] = inner
>>> outer
{'another_key': {}, 'inner_key': {}}
>>> inner['foo'] = 'spam'
>>> outer
{'another_key': {'foo': 'spam'}, 'inner_key': {'foo': 'spam'}}
请注意在打印inner
字典时如何反映对outer
的任何更改,因为两者都共享同一个对象。添加更多键指向这一个字典只创建了更多的引用,而不是副本:
>>> id(inner), id(outer['inner_key']), id(outer['another_key'])
(4413825104, 4413825104, 4413825104)
>>> inner is outer['inner_key']
True
>>> inner is outer['another_key']
True
使用新词典:
logs = {}
for d in e:
count +=1
new_dict = {'email': d["sent_to"], 'log_id': d["unique_arguments"]}
logs[count] = new_dict
另一种选择是使用dict.copy()
创建字典的(浅)副本,但由于你完全替换 这两个密钥,所以这里没有任何意义。