当两个 OrderedDicts 中的键相同时如何添加两个 OrderedDict?
How to add two OrderedDict when key is same in both the OrderedDicts?
我第一个订购的 Dic 是
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中,key是相同的,value是不同的。如何合并两个 orderedDicts 中相同键的值并创建一个新的值?
这是它的代码:
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()
更新字典本身和 returns None
.
我第一个订购的 Dic 是
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中,key是相同的,value是不同的。如何合并两个 orderedDicts 中相同键的值并创建一个新的值?
这是它的代码:
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()
更新字典本身和 returns None
.