Convert each character into a string into a dictionary key

Suppose I have the string "abcdefghijklmnopqrstuvwxyz" and I want to initialize the dictionary keys with these values.

 alphabet = 'abcdefghijklmnopqrstuvwxyz' alphabetDict = dict() for char in alphabet: alphabetDict[char] = 0 

Is there a better way to do this?

+8
source share
6 answers

You can use the dict.fromkeys() method -

 >>> s = 'abcdefghijklmnopqrstuvwxyz' >>> alphaDict = dict.fromkeys(s,0) >>> alphaDict {'m': 0, 'p': 0, 'i': 0, 'n': 0, 'd': 0, 'w': 0, 'k': 0, 'y': 0, 's': 0, 'b': 0, 'h': 0, 't': 0, 'u': 0, 'q': 0, 'g': 0, 'l': 0, 'e': 0, 'a': 0, 'j': 0, 'c': 0, 'o': 0, 'f': 0, 'v': 0, 'x': 0, 'z': 0, 'r': 0} 

From the documentation -

fromkeys (seq [, value])

Create a new dictionary with keys from seq and values ​​set to value.

fromkeys() is a class method that returns a new dictionary. The default value is None .

Note that you should not use this if value is something mutable, like list or another dict , etc. Because the value is only called once when you call the fromkeys() method, and all the keys point to the same object.

You can use this for immutable types as a value, e.g. int , str , etc.


In addition, you do not need to specify the string s or alphabet , you can use string.ascii_lowercase . Example -

 >>> import string >>> alphaDict = dict.fromkeys(string.ascii_lowercase,0) >>> alphaDict {'m': 0, 'p': 0, 'i': 0, 'n': 0, 'd': 0, 'w': 0, 'k': 0, 'y': 0, 's': 0, 'b': 0, 'h': 0, 't': 0, 'u': 0, 'q': 0, 'g': 0, 'l': 0, 'e': 0, 'a': 0, 'j': 0, 'c': 0, 'o': 0, 'f': 0, 'v': 0, 'x': 0, 'z': 0, 'r': 0} 
+13
source

You can use dictionary understanding in Python.

 alphabetDict = {char: 0 for char in alphabet} 

Dictionaries (Python Docs)

There is a slight difference between this answer and Anand above. Dict understands the value for each key, and fromkeys does this only once. If you use things like ints, this is not a problem. However, if you do

 d = {key: [] for key in <some set>} d[3].append(5) print(d[2]) 

gives you

 [] 

and you have different lists as well

 d = dict.fromkeys(<some set>, []) d[3].append(5) print(d[2]) 

gives you

 [5] 

will display all keys in the same list.

+5
source

Yes, you can do this on one line using a dictionary.

 In [1]: alphabet = 'abcdefghijklmnopqrstuvwxyz' In [2]: {x:0 for x in alphabet} # dictionary comprehension Out[2]: {'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} 
+2
source

Tried with a different approach.

 dict(zip(alphabets, '0'*len(alphabets))) 
0
source

If you need a dictionary with different values ​​instead of a constant value, you can create it as shown below using a random module:

 >>> import random >>> alphabet = 'abcdefghijklmnopqrstuvwxyz' >>> my_dict = dict([ (ch, random.randint(1,len(alphabet)) ) for ch in alphabet ] ) >>> my_dict {'a': 17, 'b': 15, 'c': 3, 'd': 5, 'e': 5, 'f': 13, 'g': 7, 'h': 1, 'i': 3, 'j': 12, 'k': 11, 'l': 7, 'm': 8, 'n': 23, 'o': 15, 'p': 7, 'q': 9, 'r': 19, 's': 17, 't': 22, 'u': 20, 'v': 24, 'w': 26, 'x': 14, 'y': 7, 'z': 24} >>> 

I create dictionaries as above when I need a dictionary with random values ​​for testing purposes.

Another way to create a dictionary with each character is text with a number of characters.

 >>> char_count = lambda text, char: text.count(char) >>> text = "Genesis 1 - 1 In the beginning God created the heavens and the earth. 2 Now the earth was formless and desolate, and there was darkness upon the surface of the watery deep, and God active force was moving about over the surface of the waters." >>> my_dict = dict( [ ( char, char_count(text, char) ) for char in text ] ) >>> my_dict {'G': 3, 'e': 32, 'n': 13, 's': 15, 'i': 5, ' ': 45, '1': 2, '-': 1, 'I': 1, 't': 17, 'h': 12, 'b': 2, 'g': 3, 'o': 12, 'd': 10, 'c': 5, 'r': 12, 'a': 19, 'v': 4, '.': 2, '2': 1, 'N': 1, 'w': 6, 'f': 6, 'm': 2, 'l': 2, ',': 2, 'k': 1, 'u': 4, 'p': 2, 'y': 1, "'": 1} 

Explanation:
1. The lambda function counts the number of occurrences of a character.
2. Call the lambda function for each character in the text to get a counter for that specific character.

Note. You can improve this code to avoid repeated calls for repeated characters.

Using vocabulary understanding can be simpler than all of the above:

 { char:(text.count(char)) for char in text } 
0
source

To avoid duplication, as @Robert Ranjan mentioned, we do it as follows

 >>> char_count = lambda text, char: text.count(char) >>> alphabet = 'abcdefghijklmnopqrstuvwxyz' >>> text = "Genesis 1 - 1 In the beginning God created the heavens and the earth. 2 Now the earth was formless and desolate, and there was darkness upon the surface of the watery deep, and God active force was moving about over the surface of the waters." >>> my_dict = dict( [ ( char, char_count(text, char) ) for char in alphabet] ) >>> my_dict {'a': 19, 'b': 2, 'c': 5, 'd': 10, 'e': 32, 'f': 6, 'g': 3, 'h': 12, 'i': 5, 'j': 0, 'k': 1, 'l': 2, 'm': 2, 'n': 13, 'o': 12, 'p': 2, 'q': 0, 'r': 12, 's': 15, 't': 17, 'u': 4, 'v': 4, 'w': 6, 'x': 0, 'y': 1, 'z': 0} >>> 
0
source

All Articles