合并两个具有相同id键的字典列表。

huangapple go评论95阅读模式
英文:

Merging two lists of dictionaries using common id key

问题

我有以下两个字典列表:

list_1 = [
   {'id': '1', 'name': 'Johnny Johson1'},
   {'id': '2', 'name': 'Johnny Johson2'},
   {'id': '1', 'name': 'Johnny Johson1'},
   {'id': '3', 'name': 'Johnny Johson3'},
]

list_2 = [
   {'id': '1', 'datetime': '2020-01-06T12:30:00.000Z'},
   {'id': '2', 'datetime': '2020-01-06T14:00:00.000Z'},
   {'id': '1', 'datetime': '2020-01-06T15:30:00.000Z'},
   {'id': '3', 'datetime': '2020-01-06T15:30:00.000Z'},
]

基本上,我希望在重复的ID上不丢失任何数据,因为它们代表不同的事件(虽然有单独的ID表示事件,但为了演示问题,不需要)。如果在一个列表中有任何ID,在另一个列表中不存在,那么完全忽略该ID。

理想情况下,我希望最终得到以下结果(从两个列表的合并中得到):

list_3 = [
   {'id': '1', 'name': 'Johnny Johson1', 'datetime': '2020-01-06T12:30:00.000Z'},
   {'id': '2', 'name': 'Johnny Johson2', 'datetime': '2020-01-06T14:00:00.000Z'},
   {'id': '1', 'name': 'Johnny Johson1', 'datetime': '2020-01-06T15:30:00.000Z'},
   {'id': '3', 'name': 'Johnny Johson3', 'datetime': '2020-01-06T15:30:00.000Z'},
]
英文:

I have the following two lists of dictionaries:

list_1 = [
   {'id': '1', 'name': 'Johnny Johson1'},
   {'id': '2', 'name': 'Johnny Johson2'},
   {'id': '1', 'name': 'Johnny Johson1'},
   {'id': '3', 'name': 'Johnny Johson3'},
]
list_2 = [
   {'id': '1', 'datetime': '2020-01-06T12:30:00.000Z'},
   {'id': '2', 'datetime': '2020-01-06T14:00:00.000Z'},
   {'id': '1', 'datetime': '2020-01-06T15:30:00.000Z'},
   {'id': '3', 'datetime': '2020-01-06T15:30:00.000Z'},
]

Essentially, I would like no loss of data even on duplicate IDs, as they represent different events (there is a sepearate ID for that, but for the purpose of demonstrating the problem, is not needed). If there are any IDs in one list, not in the other, then disregard that ID all together.

Ideally, I would like to end up with the following (from the amalgamation of the two lists):

list_3 = [
   {'id': '1', 'name': 'Johnny Johson1', 'datetime': '2020-01-06T12:30:00.000Z'},
   {'id': '2', 'name': 'Johnny Johson2', 'datetime': '2020-01-06T14:00:00.000Z'},
   {'id': '1', 'name': 'Johnny Johson1', 'datetime': '2020-01-06T15:30:00.000Z'},
   {'id': '3', 'name': 'Johnny Johson3', 'datetime': '2020-01-06T15:30:00.000Z'},
]

答案1

得分: 2

你可以使用以下的列表理解式,它使用了双星号关键字参数展开语法,在使用zip()获取成对元素的情况下对两个列表进行了求值。这将两个字典合并成一个。

list_3 = [{**x, **y} for x, y in zip(list_1, list_2)]

输出:

>>> list3
[{'id': '1', 'name': 'Johnny Johson1', 'datetime': '2020-01-06T12:30:00.000Z'},
 {'id': '2', 'name': 'Johnny Johson2', 'datetime': '2020-01-06T14:00:00.000Z'},
 {'id': '1', 'name': 'Johnny Johson1', 'datetime': '2020-01-06T15:30:00.000Z'},
 {'id': '3', 'name': 'Johnny Johson3', 'datetime': '2020-01-06T15:30:00.000Z'}]

请注意,这种方法需要至少Python 3.5版本。

英文:

You can use the following list comprehension, which uses the double asterisk keyword argent unpacking syntax, evaluated on both lists using pairwise elements obtained with zip(). This has the effect of combining the two dictionaries into one.

list_3 = [{**x, **y} for x, y in zip(list_1, list_2)]

Output:

>>> list3
[{'id': '1', 'name': 'Johnny Johson1', 'datetime': '2020-01-06T12:30:00.000Z'},
 {'id': '2', 'name': 'Johnny Johson2', 'datetime': '2020-01-06T14:00:00.000Z'},
 {'id': '1', 'name': 'Johnny Johson1', 'datetime': '2020-01-06T15:30:00.000Z'},
 {'id': '3', 'name': 'Johnny Johson3', 'datetime': '2020-01-06T15:30:00.000Z'}]

Note that this approach requires at least Python 3.5.

huangapple
  • 本文由 发表于 2020年1月7日 00:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定