[英]Group all values with the same identic key in list of tuples
I have a list of tuples with strings and dictionaries which looks like following:我有一个包含字符串和字典的元组列表,如下所示:
# data type: List<Tuple<string, dict>>
input_data_structure = [
('key1', {'a': 'b'}),
('key2', {'w': 'x'}),
('key1', {'c': 'd'}),
('key2', {'y': 'z'})]
I want to group alle values with the same keys.我想用相同的键对所有值进行分组。 So the result could look like this or similiar:所以结果可能看起来像这样或类似:
# data type: List<Tuple<string, List<dict>>>
result_data_structure = [
('key1', [{'a': 'b'}, {'c': 'd'}]),
('key2', [{'w': 'x'}, {'y': 'z'}])]
For me it is important to have a good data structure, where I can loop through the existing arrays of the keys to get the values like this:对我来说,拥有一个良好的数据结构很重要,我可以在其中循环遍历现有的 arrays 键以获得如下值:
for t in result:
for val in t[1]:
print(val)
Does someone has an idea how to process or transform the data?有人知道如何处理或转换数据吗? Thanks in advance!提前致谢!
You can use defaultdict
s to achieve easily this.您可以使用defaultdict
轻松实现此目的。
from collections import defaultdict
d = defaultdict(list)
for key, value in input_data_structure:
d[key].append(value)
d # defaultdict(<class 'list'>, {'key1': [{'a': 'b'}, {'c': 'd'}], 'key2': [{'w': 'x'}, {'y': 'z'}]})
If you need your output to be a list of tuples key/value, then you can just execute this line.如果您需要您的 output 作为元组键/值列表,那么您可以执行此行。
list(d.items()) # [('key1', [{'a': 'b'}, {'c': 'd'}]), ('key2', [{'w': 'x'}, {'y': 'z'}])]
A solution without import:没有导入的解决方案:
result = {}
for key, data in input_data_structure:
try:
result[key].append(data)
except KeyError:
result[key] = [data]
result = list(result.items())
This is a great place to use itertools.groupby
, but remember that it only works properly on sorted data.这是使用itertools.groupby
的好地方,但请记住,它仅适用于已排序的数据。
from itertools import groupby
from operator import itemgetter
input_data_structure = [
('key1', {'a': 'b'}),
('key2', {'w': 'x'}),
('key1', {'c': 'd'}),
('key2', {'y': 'z'})]
sorted_data = sorted(input_data_structure, key=itemgetter(0))
# [('key1', {'a': 'b'}), ('key1', {'c': 'd'}), ('key2', {'w': 'x'}), ('key2', {'y': 'z'})]
grouped_data = [(k, list(map(itemgetter(1), g))) for k, g in groupby(sorted_data, itemgetter(0))]
# [('key1', [{'a': 'b'}, {'c': 'd'}]), ('key2', [{'w': 'x'}, {'y': 'z'}])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.