【问题标题】:Convert all keys of a dictionary into lowercase [duplicate]将字典的所有键转换为小写[重复]
【发布时间】:2015-08-24 07:44:34
【问题描述】:
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}

有没有办法把所有的键都转换成小写? 注意:字典末尾有一个空格。

【问题讨论】:

    标签: python python-2.7 dictionary


    【解决方案1】:

    使用字典推导

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

    【讨论】:

      【解决方案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 , '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}

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

      【讨论】:

        【解决方案3】:
        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】:
          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
          

          【讨论】:

            【解决方案5】:
            new_dict = {}
            for letter in alphabet:
                number = alphabet[letter]
                new_dict.update({letter.lower():number})
            

            【讨论】:

              【解决方案6】:
              #!/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
              

              输出

              {'': 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,'u':21,'t':20,'w':23,'v':22, 'y': 25, 'x': 24, 'z': 26}

              【讨论】:

              • 这会将所有值也转换为小写
              猜你喜欢
              • 1970-01-01
              • 2014-11-05
              • 2018-12-04
              • 1970-01-01
              • 1970-01-01
              • 2017-08-19
              • 2022-01-19
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多