Home>
Contents

You can get the key in order of 012 ... with the following code, but what should I do with the alphabet version of this? Imagine an output where the key is abc ... xyz.
Thank you.

Corresponding source code
nums = {i: i * 2 for i in range (5)}
print (nums)
  • Answer # 1

    In numerical valuerange ()Instead ofstring.ascii_lowercaseIs easy to use.

    Personally,It is better to avoid the method of generating from the character codeI think. Since it depends on the character code system, future compatibility is not always guaranteed. I say "personally" because the ascii code is probably backwards compatible and is likely to be harmless.

    Numerical valuesfor inIf you just use it inrange ()Just do it.

    import string
    for s in string.ascii_lowercase:
        print (s)
    for num in range (10):
        print (num)

    Output

    a
    b
    c
    d
    e
    Abbreviation
    y
    z
    0
    1
    2
    3
    Four
    Five
    6
    7
    8
    9

  • Answer # 2

    dic = {}
    j = 0
    for i in range (97, 123):
        dic [chr (i)] = j
        j + = 1
    print (dic)
    # {'a': 0,'b': 1,'c': 2,'d': 3,'e': 4,'f': 5,'g': 6,'h': 7, 'i': 8,'j': 9,'k': 10,'l': 11,'m': 12,'n': 13,'o': 14,'p': 15,'q ': 16,' r': 17,'s': 18,'t': 19,'u': 20,'v': 21,'w': 22,'x': 23,'y': 24,'z': 25}

  • Answer # 3

    You can get the key in order of 012 ... with the following code, but the alphabet version of this is
    What should I do?

    ALPHA_NUM = 26
    alpha = {chr (ord ('a') + i): i * 2 for i in range (ALPHA_NUM)}
    print (alpha)

  • Answer # 4

    Reference information

    [python] Create a list of various character types
    https://qiita.com/okkn/items/3aef4458ed2269a59d63

    Alphabet range in Python
    https://stackoverflow.com/questions/16060899/alphabet-range-in-python