Map Lowercase Alphabet to Integers

Question:

i cant make a dict where to every one letter will be a value in the n + 1 format, start from 1.
this is my code:

dict1 = {x: y for x in 'abcdefghijklmnopqrstunvwxyz' for y in range(1, 20, 1)}
print(dict1)

i get it:

{'a': 19, 'b': 19, 'c': 19, 'd': 19, 'e': 19, 'f': 19, 'g': 19, 'h': 19, 'i': 19, 'j': 19, 'k': 19, 'l': 19, 'm': 19, 'n': 19, 'o': 19, 'p': 19, 'q': 19, 'r': 19, 's': 19, 't': 19, 'u': 19, 'v': 19, 'w': 19, 'y': 19, 'z': 19}

can u tell me why and how to get it: {'a': 1, 'b':2...

Asked By: tokman

||

Answers:

Use the tools provided by Python to make this easier to implement and read. First, don’t hardcode the lowercase alphabet, use the stdlib string. Next, don’t do the arithmetic yourself, it’s easy to get wrong: there are 26 letters but range(1, 20, 1) generates only 19 digits. You also have a typo in your alphabet (I assume): tunvw. Lastly, use enumerate() which generates tuples consisting of an element of a sequence and an auto-incrementing integer, and use the start keyword argument to set the starting integer value.

import string
d = {x: y for x, y in enumerate(string.ascii_lowercase, start=1)}

Your code doesn’t produce the result you want because for each integer from 1 to 19, it assigns the integer to the value for each key (‘a’, ‘b’, ‘c’, etc.). The first iteration of the outer for produces the dictionary {'a': 1, 'b': 2, 'c': 3, etc.}. Then, each subsequent iteration of the outer for overwrites these values with the new integer: 2, 3, 4, etc.

This is easy to see if we convert the comprehension to loops:

d = {}
for y in range(1, 20, 1):
    for x in 'abcdefghijklmnopqrstuvwxyz':
        d[x] = y
    print(d)
{'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1, 'h': 1, 'i': 1, 'j': 1, 'k': 1, 'l': 1, 'm': 1, 'n': 1, 'o': 1, 'p': 1, 'q': 1, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 1, 'w': 1, 'x': 1, 'y': 1, 'z': 1}
{'a': 2, 'b': 2, 'c': 2, 'd': 2, 'e': 2, 'f': 2, 'g': 2, 'h': 2, 'i': 2, 'j': 2, 'k': 2, 'l': 2, 'm': 2, 'n': 2, 'o': 2, 'p': 2, 'q': 2, 'r': 2, 's': 2, 't': 2, 'u': 2, 'v': 2, 'w': 2, 'x': 2, 'y': 2, 'z': 2}
{'a': 3, 'b': 3, 'c': 3, 'd': 3, 'e': 3, 'f': 3, 'g': 3, 'h': 3, 'i': 3, 'j': 3, 'k': 3, 'l': 3, 'm': 3, 'n': 3, 'o': 3, 'p': 3, 'q': 3, 'r': 3, 's': 3, 't': 3, 'u': 3, 'v': 3, 'w': 3, 'x': 3, 'y': 3, 'z': 3}
...
{'a': 19, 'b': 19, 'c': 19, 'd': 19, 'e': 19, 'f': 19, 'g': 19, 'h': 19, 'i': 19, 'j': 19, 'k': 19, 'l': 19, 'm': 19, 'n': 19, 'o': 19, 'p': 19, 'q': 19, 'r': 19, 's': 19, 't': 19, 'u': 19, 'v': 19, 'w': 19, 'x': 19, 'y': 19, 'z': 19}

edit: You don’t need a comprehension to do this. There is another, pretty clean solution:

import string
d = dict(enumerate(string.ascii_lowercase, start=1))

This solution relies on the dict(iterable) constructor:

Help on class dict in module builtins:

class dict(object)
 |  dict() -> new empty dictionary
 |  dict(mapping) -> new dictionary initialized from a mapping object's
 |      (key, value) pairs
 |  dict(iterable) -> new dictionary initialized as if via:
 |      d = {}
 |      for k, v in iterable:
 |          d[k] = v
Answered By: Michael Ruth

Try to zip the alphabet and the range to see what you get like that:

z1 = zip('abcdefghijklmnopqrstunvwxyz', range(1, 28))

Which will give us:

for tup in z1:
    print(tup)

Output:

('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)
('n', 22)
('v', 23)
('w', 24)
('x', 25)
('y', 26)
('z', 27)

Now you can assign each element of each tuple to x, y:

d1 = {x, y for x, y in zip('abcdefghijklmnopqrstunvwxyz', range(1, 28))}

Output:

print(d1)
{'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': 22,
 'o': 15,
 'p': 16,
 'q': 17,
 'r': 18,
 's': 19,
 't': 20,
 'u': 21,
 'v': 23,
 'w': 24,
 'x': 25,
 'y': 26,
 'z': 27}
Answered By: Diurnambule
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.