I am trying to create a dictionary to map the amount of times a letter appears to the letter of the alphabet, however I want to print the entire alphabet in the dictionary even if a letter does not appear in the list of strings. So i want the alphabet letter to be the key and the amount of times the letter occurs as the value.
The following is my code
import string
from collections import Counter
listy = ["hello","there","I","am","a","string"]
letter_count = dict( (key, 0) for key in string.ascii_lowercase )
print(dict_count)
My expected output should be
{a:2,b:0,c:0,d:0,e:3}
and so on until i reach z
I realize the key value should be something else in the list comprehension, but I simply cannot figure out what. I just don't exactly know what i can do to map the amount of times a letter occurs to the correct letter in my dictionary so I just added 0 there. Would using a dictionary comprehension be better? I am new to dictionaries, and dictionary comprehension, but a friend of mine recommended I should learn it since apparently it is a powerful tool to have so any help would be appreciated
CodePudding user response:
import string
listy = ["hello","there","I","am","a","string"]
concatenated_listy="".join(listy).lower()
letter_count = dict( (key, concatenated_listy.count(key)) for key in string.ascii_lowercase )
letter_count
Answer would be
{'a': 2, 'b': 0, 'c': 0, 'd': 0, 'e': 3, 'f': 0, 'g': 1, 'h': 2, 'i': 2, 'j': 0, 'k': 0, 'l': 2, 'm': 1, 'n': 1, 'o': 1, 'p': 0, 'q': 0, 'r': 2, 's': 1, 't': 2, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
CodePudding user response:
You can continue your suggested code with the below to read the letters one by one and add them to a histogram of letters encoded in your Dictionary:
import string
letterHist = dict((key, 0) for key in string.ascii_lowercase)
listy = ["hello","there","I","am","a","string"]
for word in listy:
for letter in word:
letterHist[letter.lower()] = 1
And the above should give you:
{'a': 2, 'b': 0, 'c': 0, 'd': 0, 'e': 3, 'f': 0, 'g': 1, 'h': 2, 'i': 2, 'j': 0, 'k': 0, 'l': 2, 'm': 1, 'n': 1, 'o': 1, 'p': 0, 'q': 0, 'r': 2, 's': 1, 't': 2, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
CodePudding user response:
You can use dict.fromkeys
:
from string import ascii_lowercase
listy = ["hello","there","I","am","a","string"]
dict_count = dict.fromkeys(ascii_lowercase, 0)
for letter in ''.join(listy).lower():
dict_count[letter] = 1
>>> dict_count
{'a': 2, 'b': 0, 'c': 0, 'd': 0, 'e': 6, 'f': 0, 'g': 1, 'h': 4, 'i': 2, 'j': 0,
'k': 0, 'l': 4, 'm': 1, 'n': 1, 'o': 2, 'p': 0, 'q': 0, 'r': 3, 's': 1, 't': 3,
'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
You could also use collectoins.Counter
instead of the for-loop:
>>> dict_count = dict.fromkeys(ascii_lowercase, 0)
>>> dict_count.update(Counter(''.join(listy).lower()))
>>> dict_count
{'a': 2, 'b': 0, 'c': 0, 'd': 0, 'e': 3, 'f': 0, 'g': 1, 'h': 2, 'i': 2, 'j': 0,
'k': 0, 'l': 2, 'm': 1, 'n': 1, 'o': 1, 'p': 0, 'q': 0, 'r': 2, 's': 1, 't': 2,
'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
Note: In this case, most approaches with dictionary comprehension will have poor performance (eg. if you use str.count
), so if you need to use dict comprehension
try combining it with collections.Counter
:
>>> alpha_count = Counter(''.join(listy).lower())
>>> dict_count = {alpha: alpha_count.get(alpha, 0) for alpha in ascii_lowercase}
>>> dict_count
{'a': 2, 'b': 0, 'c': 0, 'd': 0, 'e': 6, 'f': 0, 'g': 1, 'h': 4, 'i': 2, 'j': 0,
'k': 0, 'l': 4, 'm': 1, 'n': 1, 'o': 2, 'p': 0, 'q': 0, 'r': 3, 's': 1, 't': 3,
'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
CodePudding user response:
``
listy = ["hello","there","I","am","a","string"]
alphabet = 'abcdefghijklmnopqrstuvwxyz'
dict1 = {}
#Create a dictionary to store the number of occurrences of each of the 26 letters
#each word initially set to 0
for i in alphabet:
dict1[i] = 0
for j in listy:
for k in j.lower(): # Converted to lowercase
if k in dict1.keys():
dict1[k] = dict1.get(k,0) 1
print(dict1)
``