Python中的NameError嵌套用于列表理解的循环 [英] NameError in Python Nested for loops of List Comprehension

查看:58
本文介绍了Python中的NameError嵌套用于列表理解的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

for i in range(6):
    for j in range(i):
        j

AFAIK,在列表理解中,最右边的for是外部的,因此,我认为以下代码将起作用:

AFAIK, in list comprehension the right most for is the outer one so, I thought the following code will work:

[ j for j in range(i) for i in range(6)]

但令我惊讶的是,它抛出了NameError

But to my surprise, it throws a NameError

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'i' is not defined

我想知道为什么它不起作用.是因为python评估从 Left到Right 的表达式吗? 原因,我已经通过使用括号解决了该问题:

I wonder why it didn't work. Is it because python evaluates expression from Left to Right? Cause, I have resolved the issue by using parenthesis:

[ (j for j in range(i)) for i in range(6)]

输出一堆生成器表达式:

which outputs a bunch of generator expressions:

[<generator object <listcomp>.<genexpr> at 0x7f3b42200d00>, <generator object <listcomp>.<genexpr> at 0x7f3b42200d58>, <generator object <listcomp>.<genexpr> at 0x7f3b42200db0>, <generator object <listcomp>.<genexpr> at 0x7f3b42200e08>, <generator object <listcomp>.<genexpr> at 0x7f3b42200e60>, <generator object <listcomp>.<genexpr> at 0x7f3b42200eb8>]

要探究这些生成器表达式中的内容,我们可以将它们简单地转换为列表,即

To explore what is inside these generator expressions we can simply cast them into lists i.e.

[ list(j for j in range(i )) for i in range(6)]

并且输出符合预期:

[[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4]]

我只想知道这里真正发生了什么.

I just want to know what is really happening here.

推荐答案

正确,从左到右进行评估.要添加其他人的答案,我查找了文档中的官方解释.

Correct, it's evaluated from left to right. To add the others' answers, I looked up the official explanation in the documentation.

列表推导的格式为:

List comprehensions have the form:

 [ expression for expr in sequence1
              for expr2 in sequence2 ...
              for exprN in sequenceN
              if condition ]

for…in子句包含要迭代的序列.这 序列不必具有相同的长度,因为它们不是 并行进行迭代,但从从左向右;这被解释 在以下段落中会更清楚地说明.的要素 生成的列表将是表达式的连续值.决赛 if子句是可选的;如果存在,则仅对表达式求值, 如果条件为真,则添加到结果中.

The for…in clauses contain the sequences to be iterated over. The sequences do not have to be the same length, because they are not iterated over in parallel, but from left to right; this is explained more clearly in the following paragraphs. The elements of the generated list will be the successive values of expression. The final if clause is optional; if present, expression is only evaluated and added to the result if condition is true.

为了使语义非常清楚,列表理解是等效的 到以下Python代码:

To make the semantics very clear, a list comprehension is equivalent to the following Python code:

for expr1 in sequence1:
    for expr2 in sequence2:
    ...
        for exprN in sequenceN:
             if (condition):
                  # Append the value of
                  # the expression to the
                  # resulting list.

这篇关于Python中的NameError嵌套用于列表理解的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆