Home > database >  How to count characters from nested lists of strings inside a dictionary (Python)?
How to count characters from nested lists of strings inside a dictionary (Python)?

Time:11-18

I'm trying to count the frequency of a charater from nested lists of strings inside a dictionary. Returning, for each key, the most frequent value.

I was thinking something along the lines of:

res = {0: ['a', 'a', 'b'], 1: ['e'], 2: ['i', 'x', 'i', 'c']}
for k, v in res.items():
    # count the most frequent
print(res)

Expecting:

res = {0: 'a', 1: 'e', 2: 'i'}

CodePudding user response:

output = {k: most_frequenct(v) for k, v in data.items()}

most_frequenct could be any of the following https://www.geeksforgeeks.org/python-find-most-frequent-element-in-a-list/

Hope it helps

  • Related