如何将Python字典键/值转换为小写?
您可以通过简单地迭代Python字典键/值并根据键和值创建新字典来将其转换为小写。例如,
def lower_dict(d):
new_dict = dict((k.lower(), v.lower()) for k, v in d.items())
return new_dict
a = {'Foo': "Hello", 'Bar': "World"}
print(lower_dict(a))这将给出输出
{'foo': 'hello', 'bar': 'world'}如果只想将键小写,则可以仅调用小写。例如,
def lower_dict(d):
new_dict = dict((k.lower(), v) for k, v in d.items())
return new_dict
a = {'Foo': "Hello", 'Bar': "World"}
print(lower_dict(a))这将给出输出
{'foo': 'Hello', 'bar': 'World'}热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短