I have a list of lists as following:
G = [[1,2,3],[2,3,5],[1,2,3],[4,5,6],[4,5,6]]
How can I find count of unique lists in G?
I have a list of lists as following:
G = [[1,2,3],[2,3,5],[1,2,3],[4,5,6],[4,5,6]]
How can I find count of unique lists in G?
If you want the count of each sublist, the easiest way is to convert them to tuples and feed them to a Counter
.
from collections import Counter
G = [[1,2,3],[2,3,5],[1,2,3],[4,5,6],[4,5,6]]
print(Counter(map(tuple, G)))
# Counter({(1, 2, 3): 2, (4, 5, 6): 2, (2, 3, 5): 1})
You need the conversion to tuples because lists are not hashable, and Counter
uses a dictionary internally, which requires the keys to be hashable.