【发布时间】:2014-04-02 04:54:10
【问题描述】:
我在 python 2.6 中有两个字典列表,我想根据一个键对应于另一个键的最高值来合并它们。列表是这样的:
[{shape: square, color: red, priority: 2},
{shape: circle, color: blue, priority: 2},
{shape: triangle, color: green, priority: 2}]
[{shape: square, color: green, priority: 3},
{shape: circle, color: red, priority: 1}]
我正在尝试获得这样的输出:
[{shape: square, color: green, priority: 3},
{shape: circle, color: blue, priority: 2},
{shape: triangle, color: green, priority: 2}]
(项目的顺序并不重要。)
换句话说,我想遍历这两个列表并获取每个列表项的“颜色”、“形状”和“优先级”的字典,其中“优先级”的值对于每个列表项的值都是最高的'形状')
我断断续续地在 SO 上搜索和尝试不同的东西几天,我终于屈服于询问。我尝试了各种版本的 max、key、lambda 等,但我在这里能找到的所有线程似乎都不是我想要的。
提前致谢!
【问题讨论】:
-
列表是如何合并的?
标签: python dictionary python-2.6