Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the list of dictionaries that have the same keys. and I want to regroup them into several lists such that a value for certain attributes of the choices is equal. here is the example:

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}]

I want to cluster it according to the keys of a and b. Then the output would be the following list of the 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}]]

1 Answer

0 votes
by (36.8k points)

Sort this firstly, then use the itertools.groupby.You can 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 the function to receive the muitlple keys, you can 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'))

Want to be a master in Data Science? Enroll in this Data Science Courses 

Browse Categories

...