我的第一个orderedDic是
OrderedDict([('3LEmxb4G9q5XnP9H5653ncMAbAgbDCzz8U', {'value': 0.5, 'valueSat': 50000000, 'txid': '4c4e1609392f6744cdf9ff57614eb5f6905f39baba8f046c3b3bd5a1e6b573c8'}), ('1ErSPkXKfdVgjseia1psccr6ng4zbyLNSE', {'value': 0.01349045, 'valueSat': 1349045, 'txid': '4c4e1609392f6744cdf9ff57614eb5f6905f39baba8f046c3b3bd5a1e6b573c8'})])
我的第二个orderedDict是
OrderedDict([('3LEmxb4G9q5XnP9H5653ncMAbAgbDCzz8U', {'account_id': None, 'last_block': '', 'n_conf': 0, 'total_confirmations': 3}), ('1ErSPkXKfdVgjseia1psccr6ng4zbyLNSE', {'account_id': None, 'last_block': '', 'n_conf': 0, 'total_confirmations': 6})])
在上面两个orderedDict中,键是相同的但值是不同的。如何从orderedDicts合并相同键的值并创建一个新的?
答案 0 :(得分:1)
以下是代码:
from collections import OrderedDict
a= OrderedDict([('3LEmxb4G9q5XnP9H5653ncMAbAgbDCzz8U', {'value': 0.5, 'valueSat': 50000000, 'txid': '4c4e1609392f6744cdf9ff57614eb5f6905f39baba8f046c3b3bd5a1e6b573c8'}), ('1ErSPkXKfdVgjseia1psccr6ng4zbyLNSE', {'value': 0.01349045, 'valueSat': 1349045, 'txid': '4c4e1609392f6744cdf9ff57614eb5f6905f39baba8f046c3b3bd5a1e6b573c8'})])
b=OrderedDict([('3LEmxb4G9q5XnP9H5653ncMAbAgbDCzz8U', {'account_id': None, 'last_block': '', 'n_conf': 0, 'total_confirmations': 3}), ('1ErSPkXKfdVgjseia1psccr6ng4zbyLNSE', {'account_id': None, 'last_block': '', 'n_conf': 0, 'total_confirmations': 6})])
c=OrderedDict()
for key in a:
temp=b[key]
temp.update(a[key])
c[key]=temp
print c
您的新词典存储在c中。请记住.update()
更新字典本身并返回None
。