[Solved] counting occurrences in list of list


We have some list of lists a. We want to get counts for each of the sublists, and the total of those counts. Note that the total counts will just be the sum of the counts of the sublists.

from collections import Counter

a = [['das','sadad','asdas','das'],['das','sadad','da'],['aaa','8.9']]

def count(list_of_lists):
    counts = [Counter(sublist) for sublist in list_of_lists]
    total_count = sum(counts, Counter())
    return counts, total_count

Now if we want the counts of das in the first sublist and the number of das across all sublists we can do

counts, total = count(a)
print(counts[0]['das'])
# 2
print(total['das'])
# 3

0

solved counting occurrences in list of list