如何在OrderedDicts中的键相同时添加两个OrderedDict?(How to add two OrderedDict when key is same in both the Ordere

编程入门 行业动态 更新时间:2024-10-27 14:26:21
如何在OrderedDicts中的键相同时添加两个OrderedDict?(How to add two OrderedDict when key is same in both the OrderedDicts?)

我的第一个订购的迪克是

OrderedDict([('3LEmxb4G9q5XnP9H5653ncMAbAgbDCzz8U', {'value': 0.5, 'valueSat': 50000000, 'txid': '4c4e1609392f6744cdf9ff57614eb5f6905f39baba8f046c3b3bd5a1e6b573c8'}), ('1ErSPkXKfdVgjseia1psccr6ng4zbyLNSE', {'value': 0.01349045, 'valueSat': 1349045, 'txid': '4c4e1609392f6744cdf9ff57614eb5f6905f39baba8f046c3b3bd5a1e6b573c8'})])

我的第二个命令是

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})])

在上面的两个ord​​eredDict中,键是相同的,但值不同。 如何合并来自这两个ord​​eredDicts的相同键的值并创建一个新的?

My first orderedDic is

OrderedDict([('3LEmxb4G9q5XnP9H5653ncMAbAgbDCzz8U', {'value': 0.5, 'valueSat': 50000000, 'txid': '4c4e1609392f6744cdf9ff57614eb5f6905f39baba8f046c3b3bd5a1e6b573c8'}), ('1ErSPkXKfdVgjseia1psccr6ng4zbyLNSE', {'value': 0.01349045, 'valueSat': 1349045, 'txid': '4c4e1609392f6744cdf9ff57614eb5f6905f39baba8f046c3b3bd5a1e6b573c8'})])

My second orderedDict is

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})])

In the above two orderedDict, keys are same but values are different. How to merge the values of the same keys from both the orderedDicts and make one new?

最满意答案

这是它的代码:

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 。

Here is the code to it:

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

Your new dictionary is stored in c. Remember .update() updates the dictionary itself and returns None.

更多推荐

本文发布于:2023-07-26 17:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   如何在   OrderedDicts   key   OrderedDict

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!