I have a list of lists, which looks like
listOfLists = [
['a','b','c','d'],
['a','b'],
['a','c'],
['c','c','c','c']
]
I want to count the number of lists which have a particular element. For Example, my output should be
{'a':3,'b':2,'c':3,'d':1}
As you can see, I don't need the total count of an element. In the case of "c"
, though its total count is 5, the output is 3 as it occurs only in 3 lists.
I am using a counter to get the counts. The same can be seen below.
line_count_tags = []
for lists in lists_of_lists:
s = set()
for element in lists:
s.add(t)
lines_count_tags.append(list(s))
count = Counter([count for counts in lines_count_tags for count in counts])
So, when I print count, I get
{'a':3,'c':3,'b':2,'d':1}
I want to know if there's a much better way to accomplish my goal.