Dictionary to lowercase in Python

前端 未结 12 1105
无人共我
无人共我 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 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
    

提交回复
热议问题