Python 3 中的字典理解

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20489609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:35:13  来源:igfitidea点击:

Dictionary Comprehension in Python 3

pythonpython-3.x

提问by Jon

I found the following stack overflow post about dict comprehensions in Python2.7and Python 3+: Create a dictionary with list comprehension in Pythonstating that I can apply dictionary comprehensions like this:

我发现了以下关于 dict 推导式 in Python2.7and 的堆栈溢出帖子Python 3+在 Python 中创建一个带有列表推导式的字典,说明我可以像这样应用字典推导式:

d = {key: value for (key, value) in sequence}

I tried it in Python 3. However, it raises an exception.

我在 Python 3 中尝试过。但是,它引发了一个异常。

d = {'a':1, 'b':2, 'c':3, 'd':4}
{key : value for (key, value) in d}
{key : value for key, value in d}

Both versions raise a ValueErrorsaying that ValueError: need more than 1 value to unpack.

两个版本都提出了ValueError这样的说法ValueError: need more than 1 value to unpack

What is the easiest / the most direct way to make a dictionary comprehension in Python3?

在 Python3 中进行字典理解的最简单/最直接的方法是什么?

采纳答案by Martijn Pieters

Looping over a dictionary only yields the keys. Use d.items()to loop over both keys and values:

循环字典只会产生。使用d.items()循环遍历键和值:

{key: value for key, value in d.items()}

The ValueErrorexception you see is nota dict comprehension problem, nor is it limited to Python 3; you'd see the same problem in Python 2 or with a regular forloop:

ValueError您看到的异常不是字典理解问题,也不限于 Python 3;您会在 Python 2 或常规for循环中看到相同的问题:

>>> d = {'a':1, 'b':2, 'c':3, 'd':4}
>>> for key, value in d:
...     print key, value
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack

because each iteration there is only oneitem being yielded.

因为每次迭代只产生一个项目。

Without a transformation, {k: v for k, v in d.items()}is just a verbose and costly d.copy(); use a dict comprehension only when you do a little more with the keys or values, or use conditions or a more complex loop construct.

没有转换,{k: v for k, v in d.items()}只是冗长而昂贵d.copy();仅当您对键或值执行更多操作,或者使用条件或更复杂的循环结构时才使用字典理解。

回答by RandallShanePhD

Well said above - you can drop items in Python3 if you do it this way:

上面说得好 - 如果你这样做,你可以在 Python3 中删除项目:

{key: d[key] for key in d}

{key: d[key] 用于输入 d}

d = {'a':1, 'b':2, 'c':3, 'd':4}
z = {x: d[x] for x in d}
z
>>>{'a': 1, 'b': 2, 'c': 3, 'd': 4}

and this provides for the ability to use conditions as well

这也提供了使用条件的能力

y = {x: d[x] for x in d if d[x] > 1}
y
>>>{'b': 2, 'c': 3, 'd': 4}

Enjoy!

享受!

回答by Soumyaranjan Das

Dictionary comprehension means generating items in the dictionary by some logic:

字典理解是指通过某种逻辑在字典中生成项目:

x = {p: p*p for p in range(10)}

print(x)

y = {q: q*3 for q in range(5,15) if q%2!=0}

print(y)