在python中生成任意长度的数字升序列表
我可以调用一个返回升序数字列表的函数吗?即,function(10)
会返回[0,1,2,3,4,5,6,7,8,9]
?
Is there a function I can call that returns a list of ascending numbers? I.e., function(10)
would return [0,1,2,3,4,5,6,7,8,9]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要
range()
。You want
range()
.range(10)
是内置的。range(10)
is built in.如果您想要一个能够提供一系列不确定长度的迭代器,可以使用
itertools.count()
。这里我使用range()
进行迭代,因此循环有限制。稍后:另外,在 python 3.x 中,range() 返回一个迭代器,而不是一个列表。在这种情况下,您需要
list(range(10))
。If you want an iterator that gives you a series of indeterminate length, there is
itertools.count()
. Here I am iterating withrange()
so there is a limit to the loop.Later: also, range() returns an iterator, not a list, in python 3.x. in that case, you want
list(range(10))
.