如何将 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'}如果你仅想将键转换为小写,则可以仅对其调用 lower。例如:
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'}
广告
数据结构
网络
数据库管理系统
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP