k92
k92

Reputation: 385

Count occurrence of strings in list of lists

I want to count the number of times a string has occurred in a list which is in another list and store it in a list of dictionary where each dictionary has count of a list. Ex,

list = [['Sam','John','Alex','Sam','Alex'],['Max','Sam','Max']...]

and I want my list of dictionaries to be like:

count_list = [{'Sam':2,'Alex':2,'John':1}, {'Max':2, 'Sam':1}..] 

I am iterating through each list to count number of times each string has occurred and adding each result to dict. But I end up having different result every time and not the correct values.

count_list = []
for l in list :
    d = {}
    for str in l:
        if str not in d:
            d[str] = l.count(str)
            count_list.append(d)

Any help would be useful.Thanks.

Upvotes: 1

Views: 2147

Answers (2)

RoadRunner
RoadRunner

Reputation: 26315

It would be easier to use collections.Counter() here:

>>> from collections import Counter
>>> lst = [["Sam", "John", "Alex", "Sam", "Alex"], ["Max", "Sam", "Max"]]
>>> list(map(Counter, lst))
[Counter({'Sam': 2, 'Alex': 2, 'John': 1}), Counter({'Max': 2, 'Sam': 1})]

You could also use a list comprehension instead of using map() if thats easier to understand:

>>> [Counter(l) for l in lst]
[Counter({'Sam': 2, 'Alex': 2, 'John': 1}), Counter({'Max': 2, 'Sam': 1})]

Note: Counter is a subclass of dict, so you can treat them like normal dictionaries.

You can always cast to dict() if you want to as well:

>>> [dict(Counter(l)) for l in lst]
[{'Sam': 2, 'John': 1, 'Alex': 2}, {'Max': 2, 'Sam': 1}]

You should also not use list as a variable name, since it shadows the builtin function list().

Upvotes: 5

Andrew Fan
Andrew Fan

Reputation: 1322

Currently, you are doing the following:

count_list = []
for l in list :
    d = {}
    for str in l:
        if str not in d:
            d[str] = l.count(str)
            count_list.append(d)

Note that you are appending the dictionary for each string in the sub lists, rather than one dictionary per sub list.

Doing the following should address the issue:

count_list = []
for l in list :
    d = {}
    for str in l:
        if str not in d:
            d[str] = l.count(str)
    count_list.append(d)

Upvotes: 0

Related Questions