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

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -