I want to generate a dict with the letters of the alphabet as the keys, something like
letter_count = {'a': 0, 'b': 0, 'c': 0}
what would be a fast way of generating that dict, rather than me having to type it in?
I want to generate a dict with the letters of the alphabet as the keys, something like
letter_count = {'a': 0, 'b': 0, 'c': 0}
what would be a fast way of generating that dict, rather than me having to type it in?
I find this solution elegant:
import string
d = dict.fromkeys(string.ascii_lowercase, 0)
print(d)
# {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
{ch: n for n, ch in enumerate(string.ascii_lowercase)}
–
Elaina d = {chr(x): 0 for x in range(ord('a'), ord('z')+1)}
–
Intolerable import string
letter_count = dict(zip(string.ascii_lowercase, [0]*26))
print(letter_count)
# {'a': 0, 'b': 0, 'c': 0, ... 'x': 0, 'y': 0, 'z': 0}
or maybe:
import string
import itertools
letter_count = dict(zip(string.ascii_lowercase, itertools.repeat(0)))
print(letter_count)
# {'a': 0, 'b': 0, 'c': 0, ... 'x': 0, 'y': 0, 'z': 0}
or even:
import string
letter_count = dict.fromkeys(string.ascii_lowercase, 0)
print(letter_count)
# {'a': 0, 'b': 0, 'c': 0, ... 'x': 0, 'y': 0, 'z': 0}
The preferred solution might be a different one, depending on the actual values you want in the dict.
I'll take a guess here: do you want to count occurences of letters in a text (or something similar)? There is a better way to do this than starting with an initialized dictionary.
Use Counter
from the collections
module:
import collections
the_text = 'the quick brown fox jumps over the lazy dog'
letter_counts = collections.Counter(the_text)
print(letter_counts)
# Counter({' ': 8, 'o': 4, 'e': 3, ... 'n': 1, 'x': 1, 'k': 1, 'b': 1})
This is very easy with dictionary comprehensions:
# Generate lowercase Mapping
{chr(i+96):i for i in range(1,27)}
# Generates :
# {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's': 19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}
# Generate UPPERCASE Mapping
{chr(i+64):i for i in range(1,27)}
# Generates :
# {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10, 'K': 11, 'L': 12, 'M': 13, 'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'T': 20, 'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26}
Here's a compact version, using a list comprehension:
>>> import string
>>> letter_count = dict( (key, 0) for key in string.ascii_lowercase )
>>> letter_count
{'a': 0, 'c': 0, 'b': 0, 'e': 0, 'd': 0, 'g': 0, 'f': 0, 'i': 0, 'h': 0, 'k': 0,
'j': 0, 'm': 0, 'l': 0, 'o': 0, 'n': 0, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0,
't': 0, 'w': 0, 'v': 0, 'y': 0, 'x': 0, 'z': 0}
If you plan to use it for counting, I suggest the following:
import collections
d = collections.defaultdict(int)
Yet another 1-liner Python hack:
letter_count = dict([(chr(i),0) for i in range(97,123)])
print(letter_count)
# {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
d = {chr(x): 0 for x in range(ord('a'), ord('z')+1)}
This is more concise –
Intolerable There's this too:
import string
letter_count = dict((letter, 0) for letter in string.ascii_lowercase)
print(letter_count)
# {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
string.ascii_lowercase
. What if you've missed (typed twice) a letter? How do you tell it now, in 6 month? string.ascii_lowercase
doesn't have such problems. –
Summitry import string
letters = string.ascii_lowercase
d = dict(zip(letters, [0]*len(letters)))
print(d)
# {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
You can use dictionary and range directly, so you can create your own function and easily customize it.
def gen_alphabet(start, value):
return {chr(ord('a') + i) : 0 for i in range(value)}
print(gen_alphabet('a', 26))
OUTPUT:
>>> {'a': 0, 'c': 0, 'b': 0, 'e': 0, 'd': 0, 'g': 0, 'f': 0, 'i': 0, 'h': 0, 'k': 0, 'j': 0, 'm': 0, 'l': 0, 'o': 0, 'n': 0, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0, 't': 0, 'w': 0, 'v': 0, 'y': 0, 'x': 0, 'z': 0}
import string
s0 = string.ascii_lowercase
s1 = string.ascii_uppercase
s2 = string.ascii_letters
print(dict(enumerate(s0,1))) # {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
print(dict(enumerate(s1,1))) # {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z'}
print(dict(enumerate(s2,1))) # {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z', 27: 'A', 28: 'B', 29: 'C', 30: 'D', 31: 'E', 32: 'F', 33: 'G', 34: 'H', 35: 'I', 36: 'J', 37: 'K', 38: 'L', 39: 'M', 40: 'N', 41: 'O', 42: 'P', 43: 'Q', 44: 'R', 45: 'S', 46: 'T', 47: 'U', 48: 'V', 49: 'W', 50: 'X', 51: 'Y', 52: 'Z'}
My variant :
Note : range A-Z in unicode => 65-90 (decimal)
d = dict.fromkeys([chr(j) for j in range(65, 90)], 0)
print(d)
OUTPUT :
>>> {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0, 'H': 0, 'I': 0, 'J': 0, 'K': 0, 'L': 0, 'M': 0, 'N': 0, 'O': 0, 'P': 0, 'Q': 0, 'R': 0, 'S': 0, 'T': 0, 'U': 0, 'V': 0, 'W': 0, 'X': 0, 'Y': 0}
If you are looking for a multilingual solution for alphabets other than English, I recommend the Alphabetic library, which can be installed via: pip install alphabetic
After installing, import the library, create a WritingSystem instance and select the alphabet of the desired language (Alphabetic covers a wide range of languages):
from alphabetic import WritingSystem
ws = WritingSystem()
# English
print(dict.fromkeys(ws.by_language(ws.Language.English, as_list=True), 0))
# {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'G': 0, 'H': 0, 'I': 0, 'J': 0, 'K': 0, 'L': 0, 'M': 0, 'N': 0, 'O': 0, 'P': 0, 'Q': 0, 'R': 0, 'S': 0, 'T': 0, 'U': 0, 'V': 0, 'W': 0, 'X': 0, 'Y': 0, 'Z': 0, 'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
# Hebrew
print(dict.fromkeys(ws.by_language(ws.Language.Hebrew, as_list=True), 0))
# {'א': 0, 'ב': 0, 'ג': 0, 'ד': 0, 'ה': 0, 'ו': 0, 'ז': 0, 'ח': 0, 'ט': 0, 'י': 0, 'כ': 0, 'ך': 0, 'ל': 0, 'מ': 0, 'ם': 0, 'נ': 0, 'ן': 0, 'ס': 0, 'ע': 0, 'פ': 0, 'ף': 0, 'צ': 0, 'ץ': 0, 'ק': 0, 'ר': 0, 'ש': 0, 'ת': 0}
# Georgian
print(dict.fromkeys(ws.by_language(ws.Language.Georgian, as_list=True), 0))
# {'ა': 0, 'ბ': 0, 'გ': 0, 'დ': 0, 'ე': 0, 'ვ': 0, 'ზ': 0, 'თ': 0, 'ი': 0, 'კ': 0, 'ლ': 0, 'მ': 0, 'ნ': 0, 'ო': 0, 'პ': 0, 'ჟ': 0, 'რ': 0, 'ს': 0, 'ტ': 0, 'უ': 0, 'ფ': 0, 'ქ': 0, 'ღ': 0, 'ყ': 0, 'შ': 0, 'ჩ': 0, 'ც': 0, 'ძ': 0, 'წ': 0, 'ჭ': 0, 'ხ': 0, 'ჯ': 0, 'ჰ': 0, 'ჱ': 0, 'ჲ': 0, 'ჳ': 0, 'ჴ': 0, 'ჵ': 0}
# Korean
print(dict.fromkeys(ws.by_language(ws.Language.Korean, as_list=True), 0))
# {'ㄱ': 0, 'ㄲ': 0, 'ㄴ': 0, 'ㄷ': 0, 'ㄸ': 0, 'ㄹ': 0, 'ㅁ': 0, 'ㅂ': 0, 'ㅃ': 0, 'ㅅ': 0, 'ㅆ': 0, 'ㅇ': 0, 'ㅈ': 0, 'ㅉ': 0, 'ㅊ': 0, 'ㅋ': 0, 'ㅌ': 0, 'ㅍ': 0, 'ㅎ': 0, 'ㅏ': 0, 'ㅐ': 0, 'ㅑ': 0, 'ㅒ': 0, 'ㅓ': 0, 'ㅔ': 0, 'ㅕ': 0, 'ㅖ': 0, 'ㅗ': 0, 'ㅘ': 0, 'ㅙ': 0, 'ㅚ': 0, 'ㅛ': 0, 'ㅜ': 0, 'ㅝ': 0, 'ㅞ': 0, 'ㅟ': 0, 'ㅠ': 0, 'ㅡ': 0, 'ㅢ': 0, 'ㅣ': 0}
a_to_z = [(chr(i+64), chr(i+64)) for i in range(1, 27)]
Output
[('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D'), ('E', 'E'), ('F', 'F'), ('G', 'G'), ('H', 'H'), ('I', 'I'), ('J', 'J'), ('K', 'K'), ('L', 'L'), ('M', 'M'), ('N', 'N'), ('O', 'O'), ('P', 'P'), ('Q', 'Q'), ('R', 'R'), ('S', 'S'), ('T', 'T'), ('U', 'U'), ('V', 'V'), ('W', 'W'), ('X', 'X'), ('Y', 'Y'), ('Z', 'Z')]
Construct a dict of words from alphabetical order A to zop each words adjectives, adverbs as to be included. Convert the list of adjective and adverbs from dict to list
© 2022 - 2024 — McMap. All rights reserved.