python - Using Counter with list of lists -
how use counter in collections library convert list of lists count of number of times each word occurs overall?
e.g. [['a','b','a','c'], ['a','b','c','d']] -> {a:2, b:2, c:2, d:1}
i.e. a
,b
, c
occur in both lists d
occurs in 1 list.
using generator expression set
:
>>> collections import counter >>> seq = [['a','b','a','c'], ['a','b','c','d']] >>> counter(x xs in seq x in set(xs)) counter({'a': 2, 'c': 2, 'b': 2, 'd': 1})
responding comment, without generator expression:
>>> c = counter() >>> xs in seq: ... x in set(xs): ... c[x] += 1 ... >>> c counter({'a': 2, 'c': 2, 'b': 2, 'd': 1})
Comments
Post a Comment