“清单理解”有什么作用。和类似的意思?它如何工作,如何使用它?
我有以下代码:
[x ** 2 for x in range(10)]
当我在python shell中运行它时,它返回:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
我已经搜索了,看来这被称为 list consection ,同样似乎有设置/dict enlastions and dict enlastions和生成器表达。但是它如何工作?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
来自 文档 :
关于您的问题,列表理解与以下“普通” python代码相同的事情:
如何将其写成一行?嗯...我们可以...可能...使用
map( )
与 :但是,只使用列表理解越清楚,更简单吗?
基本上,我们可以使用
x
来做任何事情。不仅x ** 2
。例如,运行x
的方法:或使用
x
作为另一个函数的参数:例如,我们还可以使用
x
作为密钥dict
对象的of。让我们看看:组合怎么样?
等等。
您也可以在列表理解中使用或如果... else 在列表理解中使用
。例如,您只需要
范围(10)
中的奇数数字。您可以做:啊,太复杂了。那以下版本呢?
要使用
,如果... else
三元表达式,您需要将放置... else ...
之后x
, <代码>范围之后(10):您是否听说过 嵌套列表理解 ?您可以在一个列表理解中将的两个或多个
放在一个
中。例如:让我们谈谈第一部分,<代码> x在[[1,2,3],[4,5,6]] 中给出
[1,2,3] < /code>和
2 ,[4,5,6]
。然后,对于x
给出1
,3
和4
,5
,6
。警告:您始终需要在[[1,2,3],[4,5,6]]
对于i在x 中的i:
我们还设置了设置综合, dict enlastions 和 Generator Expressions 。
设置综合和列表综合基本上是相同的,但是前者返回a set 而不是 list :与
相同
: a dict classence 看起来像设置的理解,但它使用
{键:键的值,in ...}
或{i:i in ... in ...}
而不是{i for I in ...} 。
例如:
它等于:
(i在范围(5)中i)
给出 tuple ?不!返回 Generator :它与以下内容相同:
您可以将其用作生成器:
注意:如果您在函数内使用列表clastension ,您不需要
[]
如果该功能可以通过生成器循环。例如,sum()
:< strong>相关(关于生成器):了解Python中的生成器。
From the documentation:
About your question, the list comprehension does the same thing as the following "plain" Python code:
How do you write it in one line? Hmm...we can...probably...use
map()
withlambda
:But isn't it clearer and simpler to just use a list comprehension?
Basically, we can do anything with
x
. Not onlyx**2
. For example, run a method ofx
:Or use
x
as another function's argument:We can also, for example, use
x
as the key of adict
object. Let's see:How about a combination?
And so on.
You can also use
if
orif...else
in a list comprehension. For example, you only want odd numbers inrange(10)
. You can do:Ah that's too complex. What about the following version?
To use an
if...else
ternary expression, you need put theif ... else ...
afterx
, not afterrange(10)
:Have you heard about nested list comprehension? You can put two or more
for
s in one list comprehension. For example:Let's talk about the first part,
for x in [[1, 2, 3], [4, 5, 6]]
which gives[1, 2, 3]
and[4, 5, 6]
. Then,for i in x
gives1
,2
,3
and4
,5
,6
.Warning: You always need put
for x in [[1, 2, 3], [4, 5, 6]]
beforefor i in x
:We also have set comprehensions, dict comprehensions, and generator expressions.
set comprehensions and list comprehensions are basically the same, but the former returns a set instead of a list:
It's the same as:
A dict comprehension looks like a set comprehension, but it uses
{key: value for key, value in ...}
or{i: i for i in ...}
instead of{i for i in ...}
.For example:
And it equals:
Does
(i for i in range(5))
give a tuple? No!, it's a generator expression. Which returns a generator:It's the same as:
And you can use it as a generator:
Note: If you use a list comprehension inside a function, you don't need the
[]
if that function could loop over a generator. For example,sum()
:Related (about generators): Understanding Generators in Python.
有列表,字典和设置综合,但没有元组综合(尽管确实探索了“生成器表达”)。
他们解决了python中传统循环是陈述(不要返回任何内容)而不是返回值的表达式的问题。
它们不是解决每个问题的解决方案,并且可以被重写为传统循环。当需要维护状态时,它们变得尴尬&amp;在迭代之间进行了更新。
它们通常由:
但可以以许多有趣且奇怪的方式扭曲。
它们可以类似于传统
map()
和filter()
仍然存在于Python中并继续使用的操作。做得好时,它们具有很高的满足商。
There are list, dictionary, and set comprehensions, but no tuple comprehensions (though do explore "generator expressions").
They address the problem that traditional loops in Python are statements (don't return anything) not expressions which return a value.
They are not the solution to every problem and can be rewritten as traditional loops. They become awkward when state needs to be maintained & updated between iterations.
They typically consist of:
but can be twisted in lots of interesting and bizarre ways.
They can be analogous to the traditional
map()
andfilter()
operations which still exist in Python and continue to be used.When done well, they have a high satisfaction quotient.
如果您喜欢一种更视觉的方法来弄清楚发生了什么,那么也许这会有所帮助:
...产生输出
[3,4]
If you prefer a more visual way of figuring out what's going on then maybe this will help:
...produces the output
[3, 4]
最近,我看到了很多关于清单综合工作方式的困惑(在其他问题和同事上)。数学教育的一段时间可以帮助为什么语法是这样的,以及清单的综合性真正含义。
语法
最好将列表的综合视为在集合/集合上的谓词,就像我们在数学中使用集合构建器符号一样。对我来说,符号实际上很自然,因为我拥有数学学位。但是忘了我,吉多·范·罗斯姆(Python的发明者)拥有数学硕士学位,并具有数学背景。
设置构建器符号速效课程
这是设置构建器符号的工作方式的(非常基础):
所以,此集合构建器表示法代表严格肯定的数字集(即
[1,2, 3,4,...]
)。混淆点
1) set构建器符号中的谓词过滤器仅指定我们要保留的项目,并列出理解谓词做同样的事情。您不必在省略项目,除非谓词包括在内,否则将省略它们。空谓词(即最后没有条件)包括给定集合中的所有项目。
2) set Builder符号中的谓词过滤器最终出现在列表综合中。(某些)初学者认为
[x&lt; 5对于x范围(10)]
将为他们提供列表[0,1,2,3,4]
,而实际上它输出是真的,正确,正确,错误,错误,false,false,false] 。我们获得输出[真实,真实,真实,真实,真实,错误,false,false,false,false]
,因为我们要求Python评估x&lt; 5
<代码>范围(10)中的所有项目。没有谓词意味着我们从集合中获取所有内容(就像在集合构建器符号中一样)。如果您在使用列表综合的同时将设置的构建器符号保持在脑海中,则它们更容易吞咽。
hth!
I've seen a lot of confusion lately (on other SO questions and from coworkers) about how list comprehensions work. A wee bit of math education can help with why the syntax is like this, and what list comprehensions really mean.
The syntax
It's best to think of list comprehensions as predicates over a set/collection, like we would in mathematics by using set builder notation. The notation actually feels pretty natural to me, because I hold an undergrad degree in Mathematics. But forget about me, Guido van Rossum (inventor of Python) holds a masters in Mathematics and has a math background.
Set builder notation crash course
Here's the (very basics) of how set builder notation works:
So, this set builder notation represents the set of numbers that are strictly positive (i.e.
[1,2,3,4,...]
).Points of confusion
1) The predicate filter in set builder notation only specifies which items we want to keep, and list comprehension predicates do the same thing. You don't have to include special logic for omitting items, they are omitted unless included by the predicate. The empty predicate (i.e. no conditional at the end) includes all items in the given collection.
2) The predicate filter in set builder notation goes at the end, and similarly in list comprehensions. (some) Beginners think something like
[x < 5 for x in range(10)]
will give them the list[0,1,2,3,4]
, when in fact it outputs[True, True, True, True, True, False, False, False, False, False]
. We get the output[True, True, True, True, True, False, False, False, False, False]
because we asked Python to evaluatex < 5
for all items inrange(10)
. No predicate implies that we get everything from the set (just like in set builder notation).If you keep set builder notation in the back of your mind while using list comprehensions, they're a bit easier to swallow.
HTH!
简介
两个代码的两个片段都将产生
squares
等于[0,1,4,9,16]
。请注意,在第一个片段中,您的类型是在声明您想要的列表,而第二个则指定如何创建它。这就是为什么理解是高级和声明的原因。
语法
表达式
是任何python表达式,但通常具有一些变量是典型的。此变量在变量
字段中说明。sequence
定义值列举的值的来源。考虑片段1,
[n ** 2 for n范围(5)]
:expression
isn ** 2
variable
/code> is
n
sequence
是range> range(5)
请注意,如果您检查
Squares
的类型,您将了解列表理解只是一个常规列表:更多关于表达式
表达式可以是将任何降低到值的事物:
n ** 2 + 3 * n + 1 < /code>
f(n)
使用n
作为变量的s [::- 1]
一些示例:
过滤:
最终列表中元素的顺序由
sequence
的顺序确定。但是,您可以过滤出添加的元素,如果
子句:条件
是评估true
或false> false
的表达式。 。从技术上讲,条件不必依赖于变量
,但通常使用它。示例:
另外,请记住,Python允许您编写除列表以外的其他综合:
Introduction
Both snippets of code will produce
squares
to be equal to[0, 1, 4, 9, 16]
.Notice that in the first snippet, what you type is declaring what kind of list you want, while the second is specifying how to create it. This is why a comprehension is a high-level and declarative.
Syntax
EXPRESSION
is any Python expression, but it is typical to have some variable in it. This variable is stated inVARIABLE
field.SEQUENCE
defines the source of values the variable enumerates through.Considering Snippet 1,
[n ** 2 for n in range(5)]
:EXPRESSION
isn ** 2
VARIABLE
isn
SEQUENCE
isrange(5)
Notice that if you check the type of
squares
you will get that the list comprehension is just a regular list:More about EXPRESSION
The expression can be anything that reduces to a value:
n ** 2 + 3 * n + 1
f(n)
usingn
as variables[::-1]
bar.foo()
Some examples:
Filtering:
The order of elements in the final list is determined by the order of
SEQUENCE
. However, you can filter out elements adding anif
clause:CONDITION
is an expression that evaluates toTrue
orFalse
. Technically, the condition doesn't have to depend uponVARIABLE
, but it typically uses it.Examples:
Also, remember that Python allows you to write other kinds of comprehensions other than lists:
对于那些寻找A 嵌套的内联操作员 :
它等于
itertools.products.product
:对于其他上下文,这等同于以下嵌套循环:
For those looking for a nested inline operator:
Which is equivalent to
itertools.product
:For additional context, this is equivalent to the following nested loops: