简体   繁体   English

在 Python 的 For 循环中使用“与”运算符

[英]Using the 'And' Operator in a For-Loop in Python

So I'm a bit curious about why this doesn't work.所以我有点好奇为什么这不起作用。

How come code like:代码怎么来的:

for a in range(10) and b in range(10):
  print a + b

generates an error that says 'b is not defined'?生成一个错误,显示“b 未定义”?

Also, code like:此外,代码如下:

for a,b in range(10):
  print a + b

generates an error: 'int objects are not iterable'.生成错误:'int 对象不可迭代'。

Why?为什么? I haven't established their value beforehand, so how would Python know they are int objects?我没有事先确定它们的值,那么 Python 怎么知道它们是 int 对象呢? Also, I know you could use a while loop instead, but is there any way to carry out the sort of operation I'm doing using a for-loop alone?另外,我知道您可以改用 while 循环,但是有没有办法单独使用 for 循环来执行我正在执行的那种操作?

for a,b in zip(range(10),range(10)):
    print a + b

should work great... assuming I understood your question properly if not then应该很好用......假设我正确理解了你的问题,如果不是那么

for a in range(10):
    for b in range(10):
        print a+b

or even [a+b for a in range(10) for b in range(10)]甚至[a+b for a in range(10) for b in range(10)]

Other answers explained the right way to do it, but nobody explained what was wrong with what you did yet.其他答案解释了正确的方法,但没有人解释你所做的事情有什么问题。

for a in range(10) and b in range(10):
  print a + b

That's a cute idea of some intuitive syntax, but Python doesn't know that one.这是一些直观语法的好主意,但 Python 不知道。 The precedence of what you programmed actually works out like this:你编程的优先级实际上是这样的:

for a in ((range(10)) and (b in range(10))):

Python thinks you're trying to make a complex expression to generate a single iterable to iterate over. Python 认为您正在尝试创建一个复杂的表达式来生成一个可迭代的可迭代对象。 The first error occurs when it tries to evaluate b to build the value.第一个错误发生在它尝试评估 b 以构建值时。 If b was defined, then b in range(10) would result in True or False .如果定义b in range(10)将导致TrueFalse The result of anding it with range(10) will also be a boolean.range(10)相加的结果也将是一个布尔值。 Then you'd hit another error trying to iterate over a boolean.然后你会在尝试迭代布尔值时遇到另一个错误。

for a,b in range(10):
  print a + b

This kind of syntax works, if the enumeration on the right contains elements that are 2-tuples.如果右侧的枚举包含 2 元组的元素,则这种语法有效。 The first step in this for loop is the equivalent of trying a,b = 0 .这个 for 循环的第一步相当于尝试a,b = 0 It tries to "unpack" the right hand side by iterating over it.它试图通过迭代来“解包”右侧。 But you can't iterate over a single integer.但是你不能迭代一个整数。 a and b are not defined yet, but the first element of range(10) is. a 和 b 尚未定义,但 range(10) 的第一个元素已定义。 That's the integer you can't iterate over.那是您无法迭代的整数。

You can only iterate over a single iterable at a time in a for loop, the code in your question as it is, is invalid.您只能在for循环中一次迭代一个可迭代对象,您的问题中的代码实际上是无效的。 I believe this is what you intended - iterating over the two ranges simultaneously :我相信这就是您的意图 -同时迭代两个范围:

for a, b in zip(range(10), range(10)):
    print a + b

The zip function creates a single iterable of 2-element tuples, taking one element from each range, and then we can unpack each element from the tuple into separate variables. zip函数创建一个可迭代的 2 元素元组,从每个范围中获取一个元素,然后我们可以将元组中的每个元素解包到单独的变量中。

EDIT:编辑:

If you were trying to nest one loop inside the other, the standard way to do it would be this:如果您试图将一个循环嵌套在另一个循环中,那么标准的做法是:

for a in range(10):
    for b in range(10):
        print a + b

But we can achieve the same effect by using itertools.product , which looks closer to what you had in mind in the first place:但是我们可以通过使用itertools.product来实现相同的效果,它看起来更接近您最初的想法:

import itertools as it
for a, b in it.product(range(10), range(10)):
    print a + b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM