Python 迭代字典列表并合并键同时保留值
Python to iterate list of dictionary and merge keys whilst retaining values
我正在尝试遍历字典列表并合并重复键(在本例中称为 'id'),移动值(重复键 'url'),从那个重复的实例,到第一个(保留的)实例作为值列表。
所以,从这里开始:
[{'id': ‘XYZ', 'url': ‘THIS IS A URL'},
{'id': ‘XYZ', 'url': ‘THIS IS A URL'},
{'id': ‘XYZ', 'url': ‘THIS IS A URL'},
{'id': ‘ABC', 'url': ‘THIS IS A URL'},
{'id': ‘DEF', 'url': ‘THIS IS A URL'}]
为此:
[{'id': ‘XYZ', 'url': ‘THIS IS A URL', 'THIS IS A URL', 'THIS IS A URL'},
{'id': ‘ABC', 'url': ‘THIS IS A URL'},
{'id': ‘DEF', 'url': ‘THIS IS A URL'}]
您可以使用 defaultdict 将 id 映射到 url 列表,然后将其转换回原始对象。
import collections
id_to_url = collections.defaultdict(list)
urls = [{'id': 'XYZ', 'url': 'THIS IS A URL'},
{'id': 'XYZ', 'url': 'THIS IS A URL'},
{'id': 'XYZ', 'url': 'THIS IS A URL'},
{'id': 'ABC', 'url': 'THIS IS A URL'},
{'id': 'DEF', 'url': 'THIS IS A URL'}]
for url in urls:
id_to_url[url['id']].append(url['url'])
urls = [{'id': key, 'url': value} for key, value in id_to_url.items()]
结果:
>>> urls
[{'id': 'XYZ', 'url': ['THIS IS A URL', 'THIS IS A URL', 'THIS IS A URL']}, {'id': 'ABC', 'url': ['THIS IS A URL']}, {'id': 'DEF', 'url': ['THIS IS A URL']}]
我正在尝试遍历字典列表并合并重复键(在本例中称为 'id'),移动值(重复键 'url'),从那个重复的实例,到第一个(保留的)实例作为值列表。
所以,从这里开始:
[{'id': ‘XYZ', 'url': ‘THIS IS A URL'},
{'id': ‘XYZ', 'url': ‘THIS IS A URL'},
{'id': ‘XYZ', 'url': ‘THIS IS A URL'},
{'id': ‘ABC', 'url': ‘THIS IS A URL'},
{'id': ‘DEF', 'url': ‘THIS IS A URL'}]
为此:
[{'id': ‘XYZ', 'url': ‘THIS IS A URL', 'THIS IS A URL', 'THIS IS A URL'},
{'id': ‘ABC', 'url': ‘THIS IS A URL'},
{'id': ‘DEF', 'url': ‘THIS IS A URL'}]
您可以使用 defaultdict 将 id 映射到 url 列表,然后将其转换回原始对象。
import collections
id_to_url = collections.defaultdict(list)
urls = [{'id': 'XYZ', 'url': 'THIS IS A URL'},
{'id': 'XYZ', 'url': 'THIS IS A URL'},
{'id': 'XYZ', 'url': 'THIS IS A URL'},
{'id': 'ABC', 'url': 'THIS IS A URL'},
{'id': 'DEF', 'url': 'THIS IS A URL'}]
for url in urls:
id_to_url[url['id']].append(url['url'])
urls = [{'id': key, 'url': value} for key, value in id_to_url.items()]
结果:
>>> urls
[{'id': 'XYZ', 'url': ['THIS IS A URL', 'THIS IS A URL', 'THIS IS A URL']}, {'id': 'ABC', 'url': ['THIS IS A URL']}, {'id': 'DEF', 'url': ['THIS IS A URL']}]