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

时间:2015-06-09 13:07:21

标签: python python-2.7 dictionary

imageView.center = CGPointMake(x coord, y coord);

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

6 个答案:

答案 0 :(得分:35)

使用dict comprehensions

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

答案 1 :(得分:2)

只需使用理解再次运行字典并将所有键转换为小写。

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

结果

  

{'':27,'a':1,'c':3,'b':2,'e':5,'d':4,'g':7,'f':6 ,'我':9,'h':8,'k':11,'j':10,'m':13,'l':12,'o':15,'n':14,' q':17,'p':16,'s':19,'r':18,'你':21,'t':20,'w':23,'v':22,'y' :25,'x':24,'z':26}

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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())

您可以访问Solution page

答案 4 :(得分:0)

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

答案 5 :(得分:-2)

[{
    "PROJ_ID" : 10039,
    "0" : 10039
},{
    "PROJ_ID" : 10042,
    "0":10042
}] 

输出

  

{' ':27,' a':1,' c':3,' b':2,' e':5 ,' d':4,'':7,' f':6,'我':   9,' h':8,'':11,' j':10,' m':13,' l':12,' o':15,' n':14,' q':   17,' p':16,&#39 ;: 19,'':18,' u':21,' t':20,' w':23,' v':22,   ' y&#39 ;: 25,' x':24,' z':26}