You can use collections.Counter for it if you convert the inner list into tuples (list
s are not hashable - dict
s need hashable keys - f.e tuples
):
from collections import Counter
a=[[2, 5, 21,],
[2, 9, 14,],
[2, 22, 32],
[3, 10, 13],
[3, 10, 13],
[3, 10, 13]]
c = Counter( map(tuple,a) ) # shorter notation for: ( tuple(item) for item in a) )
# extract all (key,value) tuples with values > 1
for what, how_much in (x for x in c.most_common() if x[1] > 1):
# 3.6 string interpol, use "{} num {}".format(list(what),how_much) else
print(f"{list(what)} num {how_much}")
Output:
[3, 10, 13] num 3
You could also use itertools.groupby() but you would have to sort the list first:
import itertools
# to make groupby work
a.sort()
for key,items in itertools.groupby(a):
how_much = len(list(items))
if how_much > 1:
print(key, "num", how_much)
Same result. The itertools usage was loosly inspired by this answer to "how to remove dupes from list of list" when looking for dupes to this OP)