GBR24 GBR24 - 2 months ago 12x
Python Question

Triple nested dictionary comprehension?

Suppose I have a

, like this:

import pandas as pd
s = pd.Series(["hello go home bye bye", "you can't always get", "what you waaaaaaant", "apple banana carrot munch 123"])

I want to create a dictionary with individual characters as keys, and their frequencies as values. Creating these dictionaries for words in the past has been easy with the help of

from collections import Counter
c = Counter(word for row in s for word in row.lower().split())

However, I'm trying now to store individual characters and am having some issues with triple-nested dict comprehensions. Here's what I have:

c = Counter((letter for letter in word) for word for row in s for word in row.lower().split())

Which gives me a syntax error. How can I make the equivalent of the following
loop in one line?

d = {}
for row in s:
for word in row.lower().split():
for letter in word:
d[letter] += 1

vkp vkp

I think you can use

Counter([j for i in s for j in i])
Counter({'a': 16, ' ': 13, 'e': 6, 'o': 6, 'n': 5, 't': 5, 'y': 5, 'h': 4, 'l': 4, 'c': 3, 'b': 3, 'u': 3, 'w': 3, 'g': 2, 'm': 2, 'p': 2, 'r': 2, "'": 1, '1': 1, '3': 1, '2': 1, 's': 1})

to get the individual character count.