python - 循环字典列表并合并具有相同ID的字典

标签 python list python-3.x dictionary merge

我有一个 dicts 列表,其中我需要合并包含相同 ID 键/值的多个字典。我目前所做的不起作用,它只输出一个新的字典,其格式正确,但我需要一个新列表中的所有合并字典(或就地突变,我不太关心这一点) )。

列表中并没有真正规定其中可能存在的具有相同 ID 的字典数量的最小值或最大值,它是另一个变化函数的输出。

这就是我所拥有的

字典列表:

# actual ID's are longer and alphanumeric, this is for simplicity.
# dicts with same ID will also have the same 'taskConstraint', 
# but that is a side effect and can't be used as a filter
test_update_list = [
{"ID":"1","taskConstraint": "FIXT","plannedCompletionDate":"2017-07-29","constraintDate":"2017-07-29"},
{"ID":"1","taskConstraint": "FIXT","plannedStartDate":"2017-07-25","constraintDate":"2017-07-25"},
{"ID":"2","taskConstraint": "MSO","plannedCompletionDate":"2017-07-29","constraintDate":"2017-07-29"},
{"ID":"1","taskConstraint": "FIXT","noteText": "Note update text"},
{"ID":"2","taskConstraint": "MSO","noteText": "Note update text"},
{"ID":"3","taskConstraint": "MFO","noteText": "Note update text"},
{"ID":"3","taskConstraint": "MFO","plannedStartDate":"2017-07-25","constraintDate":"2017-07-25"}]

所需输出:

desired_output = [
{"ID":"1","taskConstraint":"FIXT","plannedStartDate":"2017-07-25","constraintDate":"2017-07-25","plannedCompletionDate":"2017-07-29"},
{"ID":"2","taskConstraint": "MSO","plannedCompletionDate":"2017-07-29","constraintDate":"2017-07-29","noteText": "Note update text"},
{"ID":"3","taskConstraint": "MFO","plannedStartDate":"2017-07-25","constraintDate":"2017-07-25","noteText": "Note update text"}]

到目前为止我的糟糕且不正确的尝试:

test_update_list = [
{"ID":"1","taskConstraint": "FIXT","plannedCompletionDate":"2017-07-29","constraintDate":"2017-07-29"},
{"ID":"1","taskConstraint": "FIXT","plannedStartDate":"2017-07-25","constraintDate":"2017-07-25"},
{"ID":"2","taskConstraint": "MSO","plannedCompletionDate":"2017-07-29","constraintDate":"2017-07-29"},
{"ID":"1","taskConstraint": "FIXT","noteText": "Note update text"},
{"ID":"2","taskConstraint": "MSO","noteText": "Note update text"},
{"ID":"3","taskConstraint": "MFO","noteText": "Note update text"},
{"ID":"3","taskConstraint": "MFO","plannedStartDate":"2017-07-25","constraintDate":"2017-07-25"}]

new_update_list = []

for task in test_update_list:
    if len(new_update_list) > 0 and task not in new_update_list:
        for new_task in new_update_list:
            if task['ID'] == new_task['ID']:
                new_task = { **task, **new_task }
    else:
        new_update_list.append(task)

打印(new_update_list)

输出...

[{'ID': '1', 'plannedCompletionDate': '2017-07-29', 'constraintDate': '2017-07-29', 'taskConstraint': 'FIXT'}]

最佳答案

您可以将新数据添加到 dict 而不是 list,其中键为 ID。要获取预期的字典列表,请稍后在字典上调用 .values()

>>> d = {}    
>>> for dct in test_update_list:
...     d.setdefault(dct['ID'], {}).update(dct)
...

>>> pprint(list(d.values()))
[{'ID': '1',
  'constraintDate': '2017-07-25',
  'noteText': 'Note update text',
  'plannedCompletionDate': '2017-07-29',
  'plannedStartDate': '2017-07-25',
  'taskConstraint': 'FIXT'},
 {'ID': '2',
  'constraintDate': '2017-07-29',
  'noteText': 'Note update text',
  'plannedCompletionDate': '2017-07-29',
  'taskConstraint': 'MSO'},
 {'ID': '3',
  'constraintDate': '2017-07-25',
  'noteText': 'Note update text',
  'plannedStartDate': '2017-07-25',
  'taskConstraint': 'MFO'}]

关于python - 循环字典列表并合并具有相同ID的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45351503/

相关文章:

python - 在 Python UML 类图中指定 self 参数

python - sys.modules 有任何特定的顺序吗?

python - 带有 python 列表的循环引用

c# - 将 DataGridView 绑定(bind)到 List<T> 不显示数据

python - GTK 对话框边距不起作用

python - len(browser.find_elements_by_class_name ('foo' )) 给出 0

python - 关于用 python 抓取 html 的说明

python - 如何按日期过滤以从查询集中获取本周与上周的计数?

Python:排序的文件列表

Python - 创建 self 复制的类对象实例