Dictionary to lowercase in Python

前端 未结 12 1075
无人共我
无人共我 2020-12-03 02:10

I wish to do this but for a dictionary:

\"My string\".lower()

Is there a built in function or should I use a loop?

相关标签:
12条回答
  • 2020-12-03 02:59

    The following is identical to Rick Copeland's answer, just written without a using generator expression:

    outdict = {}
    for k, v in {'My Key': 'My Value'}.iteritems():
        outdict[k.lower()] = v.lower()
    

    Generator-expressions, list comprehension's and (in Python 2.7 and higher) dict comprehension's are basically ways of rewriting loops.

    In Python 2.7+, you can use a dictionary comprehension (it's a single line of code, but you can reformat them to make it more readable):

    {k.lower():v.lower()
        for k, v in
        {'My Key': 'My Value'}.items()
    }
    

    They are quite often tidier than the loop equivalent, as you don't have to initialise an empty dict/list/etc.. but, if you need to do anything more than a single function/method call they can quickly become messy.

    0 讨论(0)
  • 2020-12-03 03:01

    You will need to use either a loop or a list/generator comprehension. If you want to lowercase all the keys and values, you can do this::

    dict((k.lower(), v.lower()) for k,v in {'My Key':'My Value'}.iteritems())
    

    If you want to lowercase just the keys, you can do this::

    dict((k.lower(), v) for k,v in {'My Key':'My Value'}.iteritems())
    

    Generator expressions (used above) are often useful in building dictionaries; I use them all the time. All the expressivity of a loop comprehension with none of the memory overhead.

    0 讨论(0)
  • 2020-12-03 03:03
    def convert_to_lower_case(data):
        if type(data) is dict:
            for k, v in data.iteritems():
                if type(v) is str:
                    data[k] = v.lower()
                elif type(v) is list:
                    data[k] = [x.lower() for x in v]
                elif type(v) is dict:
                    data[k] = convert_to_lower_case(v)
        return data
    
    0 讨论(0)
  • 2020-12-03 03:04

    Shorter way in python 3: {k.lower(): v for k, v in my_dict.items()}

    0 讨论(0)
  • 2020-12-03 03:06

    I used JSON library to deserialize the dictionary first, apply lower case than convert back to dictionary

    import json
    mydict = {'UPPERCASE': 'camelValue'}
    print(mydict)
    mydict_in_str = json.dumps(mydict)
    mydict_lowercase = json.loads(mydict_in_str.lower())
    print(mydict_lowercase)
    
    0 讨论(0)
  • 2020-12-03 03:08

    Love the way you can use multilevel functions, here's my way of lowercase-ing the keys

    def to_lower(dictionary):
    
        def try_iterate(k):
            return lower_by_level(k) if isinstance(k, dict) else k
    
        def try_lower(k):
            return k.lower() if isinstance(k, str) else k
    
        def lower_by_level(data):
            return dict((try_lower(k), try_iterate(v)) for k, v in data.items())
    
        return lower_by_level(dictionary)
    
    0 讨论(0)
提交回复
热议问题