在python中生成任意长度的数字升序列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4108341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generating an ascending list of numbers of arbitrary length in python
提问by Patrick
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]?
有没有我可以调用的函数来返回一个升序数字列表?即,function(10)会回来[0,1,2,3,4,5,6,7,8,9]吗?
回答by Ned Batchelder
range(10)is built in.
range(10)是内置的。
回答by hughdbrown
If you want an iterator that gives you a series of indeterminate length, there is itertools.count(). Here I am iterating with range()so there is a limit to the loop.
如果你想要一个给你一系列不确定长度的迭代器,有itertools.count(). 在这里,我正在迭代,range()所以循环是有限制的。
>>> import itertools
>>> for x, y in zip(range(10), itertools.count()):
... print x, y
...
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
Later: also, range() returns an iterator, not a list, in python 3.x. in that case, you want list(range(10)).
后来:同样,range() 在 python 3.x 中返回一个迭代器,而不是一个列表。在那种情况下,你想要list(range(10)).

