Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中从1循环到无穷大_Python_Loops - Fatal编程技术网

Python中从1循环到无穷大

Python中从1循环到无穷大,python,loops,Python,Loops,在C中,我会这样做: int i; for (i = 0;; i++) if (thereIsAReasonToBreak(i)) break; 如何在Python中实现类似的功能?使用: 在Python2中,range()和xrange()仅限于。在Python中,3可以更高,但不是无限大: import sys for i in range(sys.maxsize**10): # you could go even higher if you really want

在C中,我会这样做:

int i;
for (i = 0;; i++)
  if (thereIsAReasonToBreak(i))
    break;
如何在Python中实现类似的功能?

使用:

在Python2中,
range()
xrange()
仅限于。在Python中,3可以更高,但不是无限大:

import sys
for i in range(sys.maxsize**10):  # you could go even higher if you really want
    if there_is_a_reason_to_break(i):
        break
因此,最好使用
count()

i = 0
while not there_is_reason_to_break(i):
    # some code here
    i += 1
选择与Python中的C代码最接近的类比可能很有诱惑力:

from itertools import count

for i in count():
    if thereIsAReasonToBreak(i):
        break

但是要注意,修改
i
不会像在C中那样影响循环的流程。因此,在将C代码移植到Python时,使用
while
循环实际上是一个更合适的选择。

如果您在C中这样做,那么您的判断就和在Python中一样含糊不清:-)

def to_infinity():
    index = 0
    while True:
        yield index
        index += 1

for i in to_infinity():
    if i > 10:
        break
对于在每次迭代开始时通过简单条件检查退出的循环,在循环构造本身中这样做更为常见(在我看来更为清晰)。换句话说,类似于(如果在循环结束后需要
i
):

或者(如果
i
的范围仅限于循环):


这将转化为Python的等价物:

i = 0
while not there_is_a_reason_to_break(i):
    # do something
    i += 1

只有当你需要在某个循环中间退出(或者如果你的条件足够复杂,它会使你的循环语句远不可读),你就需要担心破解。


当您的潜在退出在循环开始时是一个简单的退出(如图所示),通常最好将退出编码到循环本身中。

重申thg435的评论:

from itertools import takewhile, count

def thereIsAReasonToContinue(i):
    return not thereIsAReasonToBreak(i)

for i in takewhile(thereIsAReasonToContinue, count()):
    pass # or something else
或者更简洁地说:

from itertools import takewhile, count

for i in takewhile(lambda x : not thereIsAReasonToBreak(x), count()):
    pass # or something else

takewhile
模拟了一个“行为良好”的C for循环:您有一个连续条件,但有一个生成器,而不是任意表达式。在C for循环中,您可以做一些“行为不好”的事情,例如修改循环体中的
i
。如果生成器是某个局部变量
i
的闭包,那么也可以使用
takewhile
来模拟这些变量。在某种程度上,定义该闭包会使您特别明显地发现您正在做一些可能会使您的控制结构混乱的事情。

如果您想使用
for
循环,可以组合内置函数(另请参见)和具有计数器的无限
for
循环。我们使用
iter
创建一个无限迭代器,
enumerate
提供计数循环变量。默认情况下,起始值为零,但您可以使用
start
参数设置不同的起始值

def infinity():
    i=0
    while True:
        i+=1
        yield i


for i in infinity():
    if there_is_a_reason_to_break(i):
        break
对于i,在枚举中(iter(bool,True),start=1):
投入(一)
其中打印:

1
2
3
4
5
...

我不是python专家,但
while(true):如果需要(I)break I=I+1
应该可以工作吗?在C中可能会出现重复,这会导致溢出,而不会转到“无穷大”这个问题的标题不应该改为“python中从0循环到无穷大”吗?您只是想拥有一个“无限的
范围”
,这样您就可以避免
而为True:i+=1
这也可以很好地与
takewhile
配合使用:
对于takewhile中的x(因此有理由继续,count()):
对于我来说,
对于范围内的i(sys.maxint)
使用
内存错误
。您还提到了
xrange()
这一功能。@scai在Python3
range
中取代了Python2的
xrange
。在Python2
range
中,创建并返回一个实际的整数列表。你没有足够的内存来放这么大的列表谢谢,这正是我要找的。我认为这是最干净、最好的方法。是的,非常干净,除了惰性序列生成器之外,我还没有完全了解
yield
的功能,但是我的一个朋友最近使用它来提供
pushd/popd
函数,而不必维护显式堆栈。非常聪明。
from itertools import takewhile, count

for i in takewhile(lambda x : not thereIsAReasonToBreak(x), count()):
    pass # or something else
def infinity():
    i=0
    while True:
        i+=1
        yield i


for i in infinity():
    if there_is_a_reason_to_break(i):
        break
1
2
3
4
5
...