如何在列表理解中将字典值转换为小写?

How to convert the dictionary values to lower case in list comprehension?

我有一个字典列表,

list_dict = [{'name':'Rita' , 'customer_id': 'A12B1', 'city': 'Chennai'}, 
             {'name':'Sita' , 'customer_id': 'A61B8', 'city': 'Salem'}]

我需要得到结果,

list_dict = [{'name':'rita' , 'customer_id': 'a12b1', 'city': 'chennai'}, 
             {'name':'sita' , 'customer_id': 'a61b8', 'city': 'salem'}]

我试过了,

new_list = []
for index in range(len(list_dict)):
    new_dict = {}
    for key,val in list_dict[index].items():
        new_dict[key] = str(val).lower()
new_list.append(new_dict)

如何使用列表推导式获得相同的结果?

我认为这会解决您的问题:

[{ key: str(value).lower() for key, value in e.items() } for e in list_dict ]

基本上,您必须使用包含字典理解的列表理解。

list_dict = [ { k:v.lower() for k,v in d.items() } for d in list_dict ]

基本上,您必须使用包含字典理解的列表理解。

[ { k:v.lower() for k,v in s.items() } for s in list_dict ]