循环列表的dicts并组合具有相同ID的dicts

时间:2017-07-27 12:57:50

标签: python list python-3.x dictionary merge

我有一个dicts列表,我需要合并包含相同ID键/ val的多个dicts。我目前正在做的是没有工作,它只输出一个新的dict,格式正确,但我需要在一个新的列表中的所有合并的dicts(或变异到位,我不太关心这个)。

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

这就是我所拥有的

dicts列表:

# 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)

print(new_update_list)

输出......

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

1 个答案:

答案 0 :(得分:3)

您可以将新数据添加到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'}]