Mar*_*ers 17
你正在寻找一个python生成器:
def infinitenumbers():
count = 0
while True:
yield count
count += 1
Run Code Online (Sandbox Code Playgroud)
该itertools套装配有预制count发电机.
>>> import itertools
>>> c = itertools.count()
>>> next(c)
0
>>> next(c)
1
>>> for i in itertools.islice(c, 5):
... print i
...
2
3
4
5
6
Run Code Online (Sandbox Code Playgroud)