python是否具有无限整数系列的本机可迭代?
我试过range(float('inf'))和iter(int),但既不工作.
我显然可以实现自己的发电机
def int_series(next=1):
while True:
next += 1
yield next
Run Code Online (Sandbox Code Playgroud)
但这感觉就像应该存在的东西.
use*_*ica 37
是.它是itertools.count:
>>> import itertools
>>> x = itertools.count()
>>> next(x)
0
>>> next(x)
1
>>> next(x)
2
>>> # And so on...
Run Code Online (Sandbox Code Playgroud)
您可以指定start和step参数,但stop不是一个选项(这是什么xrange):
>>> x = itertools.count(3, 5)
>>> next(x)
3
>>> next(x)
8
>>> next(x)
13
Run Code Online (Sandbox Code Playgroud)
Ash*_*ary 15
你可以用itertools.count它.
for x in itertools.count():
# do something with x infinite times
Run Code Online (Sandbox Code Playgroud)
如果您不想使用返回的整数count(),那么最好使用itertools.repeat:
for _ in itertools.repeat(None):
# do something infinite times
Run Code Online (Sandbox Code Playgroud)