[英]clustering a list of dictionaries according to specific key/value pairs
Say i have a list of dictionaries that all have the same keys.假设我有一个字典列表,它们都具有相同的键。 and i want to regroup these into several lists such that the values for certain attributes of my choosing are equal.
我想将这些重新组合成几个列表,以便我选择的某些属性的值相等。 here is an example:
这是一个例子:
Suppose i have the following list of dictionaries:假设我有以下字典列表:
[ {'a': 0.0, 'b': 0.2, 'c': 0.1},
{'a': 0.1, 'b': 0.7, 'c': 0.2},
{'a': 0.0, 'b': 0.2, 'c': 0.3},
{'a': 0.1, 'b': 0.7, 'c': 0.4},
{'a': 0.0, 'b': 0.7, 'c': 0.5},
{'a': 0.0, 'b': 0.7, 'c': 0.6}]
and i want to cluster it according the the keys a and b.我想根据键 a 和 b 对它进行聚类。 Then the output would be the following list of list of dictionaries:
然后输出将是以下字典列表列表:
[[{'a': 0.0, 'b': 0.2, 'c': 0.1},
{'a': 0.0, 'b': 0.2, 'c': 0.3}]
[{'a': 0.1, 'b': 0.7, 'c': 0.2},
{'a': 0.1, 'b': 0.7, 'c': 0.4}]
[{'a': 0.0, 'b': 0.7, 'c': 0.5},
{'a': 0.0, 'b': 0.7, 'c': 0.6}]]
What is the best way of achieving this?实现这一目标的最佳方法是什么?
Sort this firstly, then use itertools.groupby
.You may could try this below:首先排序,然后使用
itertools.groupby
。您可以在下面尝试:
from itertools import groupby
t = [{'a': 0.0, 'b': 0.2, 'c': 0.1},
{'a': 0.1, 'b': 0.7, 'c': 0.2},
{'a': 0.0, 'b': 0.2, 'c': 0.3},
{'a': 0.1, 'b': 0.7, 'c': 0.4},
{'a': 0.0, 'b': 0.7, 'c': 0.5},
{'a': 0.0, 'b': 0.7, 'c': 0.6}]
print([[*j] for i, j in groupby(sorted(t, key=lambda x: (x['a'], x['b'])), key=lambda x: (x['a'], x['b']))])
Result:结果:
[[{'a': 0.0, 'b': 0.2, 'c': 0.1}, {'a': 0.0, 'b': 0.2, 'c': 0.3}], [{'a': 0.0, 'b': 0.7, 'c': 0.5}, {'a': 0.0, 'b': 0.7, 'c': 0.6}], [{'a': 0.1, 'b': 0.7, 'c': 0.2}, {'a': 0.1, 'b': 0.7, 'c': 0.4}]]
If you want to create a function to receive muitlple keys, you could try:如果你想创建一个接收多个键的函数,你可以尝试:
from itertools import groupby
def group_by(*args):
return [[*j] for i, j in groupby(sorted(t, key=itemgetter(*args)), key=itemgetter(*args))]
t = [{'a': 0.0, 'b': 0.2, 'c': 0.1},
{'a': 0.1, 'b': 0.7, 'c': 0.2},
{'a': 0.0, 'b': 0.2, 'c': 0.3},
{'a': 0.1, 'b': 0.7, 'c': 0.4},
{'a': 0.0, 'b': 0.7, 'c': 0.5},
{'a': 0.0, 'b': 0.7, 'c': 0.6}]
print(group_by('a', 'b'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.