count occurrence of a list in a list of lists
Solution 1:
Just use Counter
from collections
:
from collections import Counter
A = [[x,y],[a,b],[c,f],[e,f],[a,b],[x,y]]
new_A = map(tuple, A) #must convert to tuple because list is an unhashable type
final_count = Counter(new_A)
#final output:
for i in set(A):
print i, "=", final_count(tuple(i))
Solution 2:
You can use collections.Counter
- a dict subclass - to take the counts. First, convert the sublists to tuples to make them usable (i.e. hashable) as dictionary keys, then count:
from collections import Counter
count = Counter(map(tuple, A))