I have a list, for example:
res = [[\'a\', \'b\', \'a\'], [\'a\', \'b\', \'c\'], [\'a\']]
I want to count how many lists contains a specific le
A simple list comprehension does the trick.
>>> L=[['a', 'b', 'a'], ['a', 'b', 'c'], ['a']]
>>> ['a' in x for x in L]
[True, True, True]
>>> ['b' in x for x in L]
[True, True, False]
Using the knowledge that True
is considered 1
:
>>> sum('a' in x for x in L)
3
>>> sum('b' in x for x in L)
2
>>> sum('c' in x for x in L)
1