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

标签 python python-3.x list dictionary lowercase

我有一个字典列表,

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 ]

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

关于python - 如何在列表理解中将字典值转换为小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55701139/

相关文章:

python - 从 xlwings 返回值到 vba

python-3.x - Python Pandas DataFrame 从 0 重新索引某个数据

Python mock Patch os.environ 和返回值

python - Pyqt5 鼠标事件不适用于我的自定义选项卡栏

c# - 如何使用列表对象中 3 个属性的搜索条件加快 List<> 上的此 linq 查询

python - 在 python 中使用 BeautifulSoup 进行标签和字符串混合查找和替换

python - 在 Python 中寻找用于基本图像文件 I/O 和处理的 PIL 的更好替代方案?

Python:返回列表不起作用

java - 要映射的动态字符串列表

c++ - 如何在接触时从两个列表中删除元素?