python - 比较python中的两个字典以获得相似键的最大值

标签 python dictionary compare

我有这两个命令:

a={"test1":90,  "test2":45,  "test3":67,  "test4":74}
b={"test1":32,  "test2":45,  "test3":82,  "test4":100}

如何提取相同键的最大值以获得新的字典,如下所示:

c={"test1":90,  "test2":45,  "test3":82,  "test4":100}

最佳答案

你可以这样试试,

>>> a={"test1":90, "test2":45, "test3":67, "test4":74} 
>>> b={"test1":32, "test2":45, "test3":82, "test4":100}
>>> c = { key:max(value,b[key]) for key, value in a.iteritems() }
>>> c
{'test1': 90, 'test3': 82, 'test2': 45, 'test4': 100}

关于python - 比较python中的两个字典以获得相似键的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25658540/

相关文章:

python - Python类意外行为

python - pocketsphinx python gstreamer 音频速率

python - 如何使用 boto 从作业中提取 FreeText 答案

google-maps - 如何将 DOM 对象(图像)(位于 之外)拖到 map 中,然后在那里创建标记?

compare - AppleScript:在值大于 0 之前不要让用户继续

python - 如何使用 python astropy 将 arcsec 转换为 Mpc?

python - 通过字典调用类方法

excel - 将数据表添加到字典excel vba

python - 比较数据框和列表中的值

arm - 使用 NEON 指令与零进行比较