将字典中的所有值设为小写
Make all values in a dictionary lower-case
Python
如何使用字典(仅包含字符串)并将该字典中的所有值设为小写?
my_dict ={"first":"ABC","second":"abC","third":"aBc"}
for i in my_dict.keys():
my_dict[i] = my_dict[i].lower()
更新(对于 list
作为字典值):
my_dict ={"first":"ABC","second":"abC","third":"aBc","fourth":["Avd","Dfe"]}
for i in my_dict.keys():
if type(my_dict[i]) is list:
my_dict[i]= [j.lower() for j in my_dict[i]]
else:
my_dict[i] = my_dict[i].lower()
print(my_dict)
输出:
{'first': 'abc', 'second': 'abc', 'third': 'abc', 'fourth': ['avd', 'dfe']}
Python
如何使用字典(仅包含字符串)并将该字典中的所有值设为小写?
my_dict ={"first":"ABC","second":"abC","third":"aBc"}
for i in my_dict.keys():
my_dict[i] = my_dict[i].lower()
更新(对于 list
作为字典值):
my_dict ={"first":"ABC","second":"abC","third":"aBc","fourth":["Avd","Dfe"]}
for i in my_dict.keys():
if type(my_dict[i]) is list:
my_dict[i]= [j.lower() for j in my_dict[i]]
else:
my_dict[i] = my_dict[i].lower()
print(my_dict)
输出:
{'first': 'abc', 'second': 'abc', 'third': 'abc', 'fourth': ['avd', 'dfe']}