I\'ve tried using Counter and itertools, but since a list is unhasable, they don\'t work.
My data looks like this: [ [1,2,3], [2,3,4], [1,2,3] ]
I would like to
>>> from collections import Counter
>>> li=[ [1,2,3], [2,3,4], [1,2,3] ]
>>> Counter(str(e) for e in li)
Counter({'[1, 2, 3]': 2, '[2, 3, 4]': 1})
The method that you state also works as long as there are not nested mutables in each sublist (such as [ [1,2,3], [2,3,4,[11,12]], [1,2,3] ]
:
>>> Counter(tuple(e) for e in li)
Counter({(1, 2, 3): 2, (2, 3, 4): 1})
If you do have other unhasable types nested in the sub lists lists, use the str
or repr
method since that deals with all sub lists as well. Or recursively convert all to tuples (more work).