Python 将字典的所有键转换为小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30732915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:53:12  来源:igfitidea点击:

Convert all keys of a dictionary into lowercase

pythonpython-2.7dictionary

提问by HSRAO

alphabet = {'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, ' ':27}

Is there any way to convert all of the keys into lowercase? Note: There is a space at the end of the dict.

有没有办法将所有键转换为小写?注意:字典末尾有一个空格。

回答by styvane

use dict comprehensions

使用字典理解

alphabet =  {k.lower(): v for k, v in alphabet.items()}

回答by Mathias711

new_dict = {}
for letter in alphabet:
    number = alphabet[letter]
    new_dict.update({letter.lower():number})

回答by Paul Rooney

Just use a comprehension to run through the dictionary again and convert all keys to lowercase.

只需使用推导再次遍历字典并将所有键转换为小写。

alphlower = {k.lower(): v for k, v in alphabet.iteritems()}

Result

结果

{' ': 27, '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}

{'':27,'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}

If you are using python 3, then use alphabet.items()instead of iteritems.

如果您使用的是 python 3,则使用alphabet.items()而不是iteritems.

回答by heinst

alphabet = {'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, ' ':27}
newAlphabet = {}

for key, value in alphabet.iteritems():
    newAlphabet[key.lower()] = value
print newAlphabet

回答by Pralhad Narsinh Sonar

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':14, 'O':15,'P': 16,'Q': 17,'R': 18,'S':19,'T':20}
print dict((k.lower(), v) for k, v in d1.iteritems())

You may please visit Solution page

您可以访问解决方案页面

回答by Alex Ivanov

#!/usr/bin/python
# -*- coding: utf-8 -*-

import ast

alphabet = {'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, ' ':27}

s=str(alphabet).lower()

alphabet=ast.literal_eval(s)

print alphabet

OUTPUT

输出

{' ': 27, '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}

{'':27,'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}