产生序列0,1,2,3的发电机

发布于 2025-02-12 05:45:41 字数 184 浏览 0 评论 0 原文

Python是否提供了生成无尽序列0,1,2,3的函数?

实施它很容易:

def gen_range():
  count = 0
  while True:
    yield count
    count = count + 1 

但是我想,这已经存在于Python中。

Does Python provide a function that generates the endless sequence 0,1,2,3,... ?

It is easy to implement it:

def gen_range():
  count = 0
  while True:
    yield count
    count = count + 1 

But I suppose, this exists already in Python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2025-02-19 05:45:41

是的。查看 itertools.counts.counts.counts.counts.counts.counts 内置功能。正如您可以在链接的文档中阅读的那样,您可以设置起始号码以及步骤。还允许浮子数字。

您可以使用它:

from itertools import count

for n in count():
    print(n)

这将打印0、1、2、3,...(要小心!此示例直到您强迫以某种方式停止它才会停止)。

Yes it does. Check out the itertools.count built-in function. As you can read in the linked docs, you can set the starting number and also the step. Float numbers are also allowed.

Here's how you can use it:

from itertools import count

for n in count():
    print(n)

This is going to print 0, 1, 2, 3, ... (Be careful! This example won't stop until you force it to stop somehow).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文