clustering a list of dictionaries according to specific keyvalue pairs

Sort this firstly, then use itertools.groupby.You may could try this below:

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

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top