【发布时间】:2021-07-02 09:11:19
【问题描述】:
我有一个字典列表,其中每个字典的值都不同,除了 'name' 的值:
list_dicts = [{'id': 12345, 'name': 'Bobby Bobs', 'pets': ['cat']},
{'id': 678910, 'name': 'Bobby Bobs', 'pets': ['zebra']},
{'id': 111213, 'name': 'Lisa Bobs', 'pets': ['horse']},
{'id': 141516, 'name': 'Lisa Bobs', 'pets': ['rabbit']}]
我想在名称相同的情况下删除第二个字典,同时将额外的宠物值添加到第一个字典中。
想要的输出:
output_list_dicts = [{'id': 12345, 'name': 'Bobby Bobs', 'pets': ['cat', 'zebra']},
{'id': 111213, 'name': 'Lisa Bobs', 'pets': ['horse', 'rabbit']}]
我主要是在努力识别具有相同值的项目。我假设在找到这些之后,可以将这些项目“附加”到“宠物”嵌套列表中,并使用“流行”消除其他字典。
【问题讨论】:
标签: python list dictionary