Is there a fast way to generate a dict of the alphabet in Python?

前端 未结 11 1874
终归单人心
终归单人心 2020-12-23 13:05

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

11条回答
  •  孤城傲影
    2020-12-23 13:37

    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}
    

提交回复
热议问题