2024-04-29 08:20:23 发布
网友
python有无限整数序列的原生iterable吗?
我试过range(float('inf'))和iter(int),但都没用。
range(float('inf'))
iter(int)
很明显,我可以按照
def int_series(next=1): while True: next += 1 yield next
但这感觉好像已经存在了。
你可以用^{}来做这个。
for x in itertools.count(): # do something with x infinite times
如果不想使用count()返回的整数,则最好使用^{}:
count()
for _ in itertools.repeat(None): # do something infinite times
是的。是^{}:
>>> import itertools >>> x = itertools.count() >>> next(x) 0 >>> next(x) 1 >>> next(x) 2 >>> # And so on...
您可以指定start和step参数,尽管stop不是一个选项(这就是xrange的用途):
start
step
stop
xrange
>>> x = itertools.count(3, 5) >>> next(x) 3 >>> next(x) 8 >>> next(x) 13
你可以用^{} 来做这个。
如果不想使用} :
count()
返回的整数,则最好使用^{是的。是^{} :
您可以指定
start
和step
参数,尽管stop
不是一个选项(这就是xrange
的用途):相关问题 更多 >
编程相关推荐