Python中字典转小写
我希望这样做,但是对于字典:
"My string".lower()
是否有内置函数或者我应该使用循环?
I wish to do this but for a dictionary:
"My string".lower()
Is there a built in function or should I use a loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您将需要使用循环或列表/生成器理解。 如果你想小写所有的键和值,你可以这样做::
如果你只想小写键,你可以这样做::
生成器表达式(上面使用的)在构建字典时通常很有用; 我一直在使用它们。 循环理解的所有表现力,没有任何内存开销。
You will need to use either a loop or a list/generator comprehension. If you want to lowercase all the keys and values, you can do this::
If you want to lowercase just the keys, you can do this::
Generator expressions (used above) are often useful in building dictionaries; I use them all the time. All the expressivity of a loop comprehension with none of the memory overhead.
Python 3 中的更短方法:
{k.lower(): v for k, v in my_dict.items()}
Shorter way in python 3:
{k.lower(): v for k, v in my_dict.items()}
如果您想要多嵌套字典(json 格式)的键和值小写,这可能会有所帮助。
需要支持 Python 2.7 中应有的字典理解
PYTHON2.7 - 还支持 OrderedDict
PYTHON 3.6
If you want keys and values of multi-nested dictionary (json format) lowercase, this might help.
Need to have support for dict comprehensions what should be in Python 2.7
PYTHON2.7 -- also supports OrderedDict
PYTHON 3.6
以下内容与 Rick Copeland 的答案相同,只是在没有使用生成器表达式的情况下编写的:
生成器表达式、列表理解和(在 Python 2.7 及更高版本中)字典理解基本上是重写循环的方法。
在 Python 2.7+ 中,您可以使用字典理解(它是一行代码,但您可以重新格式化它们以使其更具可读性):
它们通常比等效的循环更整洁,因为您不必初始化空字典/列表/等..但是,如果您需要做的不仅仅是单个函数/方法调用,它们很快就会变得混乱。
The following is identical to Rick Copeland's answer, just written without a using generator expression:
Generator-expressions, list comprehension's and (in Python 2.7 and higher) dict comprehension's are basically ways of rewriting loops.
In Python 2.7+, you can use a dictionary comprehension (it's a single line of code, but you can reformat them to make it more readable):
They are quite often tidier than the loop equivalent, as you don't have to initialise an empty dict/list/etc.. but, if you need to do anything more than a single function/method call they can quickly become messy.
这将小写你所有的字典键。 即使您有嵌套的字典或列表。 您可以执行类似的操作来应用其他转换。
This will lowercase all your dict keys. Even if you have nested dict or lists. You can do something similar to apply other transformations.
我首先使用 JSON 库反序列化字典,应用小写而不是转换回字典
I used JSON library to deserialize the dictionary first, apply lower case than convert back to dictionary
在Python 3中:
In Python 3:
如果提供的字典有多种类型的键/值(数字、字符串等); 然后使用以下解决方案。
例如; 如果您有一个名为 mydict 的字典,如下所示
在 Python 2.x 中
在 Python 3.x 中
注意: 这对于单维字典效果很好
If the dictionary supplied have multiple type of key/values(numeric, string etc.); then use the following solution.
For example; if you have a dictionary named mydict as shown below
In Python 2.x
In Python 3.x
Note: this works good on single dimensional dictionaries
我这么晚才回答 - 因为问题被标记为
Python
。因此,我们需要针对
Python 2.x
和Python 3.x
提供解决方案,并处理非字符串键的情况。Python 2.x - 使用字典理解
演示:
Python 3.x - Python 3 中没有
iteritems()
.x演示:
I am answering this late - as the question is tagged
Python
.Hence answering with a solution for both
Python 2.x
andPython 3.x
, and also handling the case of non-string keys.Python 2.x - using dictionary comprehension
Demo:
Python 3.x - no
iteritems()
in Python 3.xDemo:
喜欢使用多级函数的方式,这是我将按键小写的方式
Love the way you can use multilevel functions, here's my way of lowercase-ing the keys
在
Python 3
中,您可以执行以下操作:在
Python 2
中,只需将.items()
替换为.iteritems()
:In
Python 3
you can do:In
Python 2
just substitute.items()
for.iteritems()
:一个选项是从 UserDict 进行子类型化,这样插入的所有数据都将具有小写键,并且您无需在访问时关心小写:
并且您可以转换原始字典:
An option is to subtype from UserDict, and that way all data inserted will have lower case key and you don't need to care about to lower case at access time:
And you can convert your original dict:
除了 @vldbnc 答案之外,如果您只想仅在 python 3.x 中的嵌套字典中小写键
In addition to @vldbnc answer, if you only want to lowercase the keys only in a nested dict in python 3.x