是否存在无限制版本range(或xrangePython 2),还是需要手动定义?例如
squares = (x*x for x in range(n))
Run Code Online (Sandbox Code Playgroud)
只能给我一个正方形的发电机(n-1)**2,我看不到任何明显的方式来打电话,range(infinity)所以它只是继续卡车'.
是否有可能在for循环中获得无限循环?
我的猜测是Python中可能存在无限循环.我想知道这个以供将来参考.
在开始用C语言学习代码之后,我总是假设for循环和while循环,其中essentialy总是等价的(因为在一个中总是可以重现只使用另一个的行为).但是在python中从for循环到while循环总是微不足道的,我找不到实现反转的方法.
在python中,有没有办法只使用for循环来重现while循环(无限循环)的行为?
这是一个使用递归生成器不起作用的解决方案(因为递归限制):
def infinite_loopy():
yield "All work and no play makes Jack a dull boy"
for x in infinite_loopy():
yield x
#here starts the supposedly infinite-loop
for x in infinite_loopy():
print(x)
Run Code Online (Sandbox Code Playgroud) 有没有办法在Python中编写无限循环?
for t in range(0,10):
if(t == 9): t= 0 # will this set t to 0 and launch infinite loop? No!
print(t)
Run Code Online (Sandbox Code Playgroud)
一般来说,有没有办法像Java中那样编写无限pythonic for循环而不使用while循环?