【发布时间】:2013-11-02 21:51:54
【问题描述】:
给定两个字典列表:
>>> lst1 = [{id: 1, x: "one"},{id: 2, x: "two"}]
>>> lst2 = [{id: 2, x: "two"}, {id: 3, x: "three"}]
>>> merge_lists_of_dicts(lst1, lst2) #merge two lists of dictionary items by the "id" key
[{id: 1, x: "one"}, {id: 2, x: "two"}, {id: 3, x: "three"}]
有什么方法可以实现merge_lists_of_dicts 根据字典项的键合并两个字典列表?
【问题讨论】:
-
如果 lst2[0] = {id: 2, x: "five"} 或 lst2[0] = {id: 2, y: "y"} 会怎样
-
如果“id”相同但值不同怎么办?,如果您的字典只有一项,为什么不使用元组?我认为你用错了字典?
-
你在用python 3吗?
-
所以,这根本不是重复的。两者是非常显着和重要的不同。
-
@InbarRose 这不是重复的。它正在合并 n 个字典的列表,而另一个正在合并 2 个字典。
标签: python list dictionary merge