使用多个范围语句的 Python 列表初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13959510/
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
Python list initialization using multiple range statements
提问by Hymand
I want one long list, say [1,2,3,4,5,15,16,17,18,19] as an example. To initialize this, I try typing:
我想要一个长长的列表,以 [1,2,3,4,5,15,16,17,18,19] 为例。为了初始化它,我尝试输入:
new_list = [range(1,6),range(15,20)]
However this doesn't do what I want, returning:
然而,这并没有做我想要的,返回:
[[1, 2, 3, 4, 5], [15, 16, 17, 18, 19]]
When I do:
当我做:
len(new_list)
It returns 2, instead of the 10 elements I wanted (since it made 2 lists inside the list). Obviously in this example I could just type out what I want, but I'm trying to do this for some odd iterated lists that go like:
它返回 2,而不是我想要的 10 个元素(因为它在列表中创建了 2 个列表)。显然,在这个例子中,我可以输入我想要的东西,但我试图为一些奇怪的迭代列表这样做:
new_list = [range(101,6284),8001,8003,8010,range(10000,12322)]
Desiring a 1-D list instead of a list of lists (or whatever it's best called). I'm guessing this is really easy and I'm missing it, but after quite a bit of searching I've come up with nothing too useful. Any ideas?
需要一维列表而不是列表列表(或任何最好的称呼)。我猜这真的很容易,而且我很想念它,但是经过相当多的搜索后,我没有找到太有用的东西。有任何想法吗?
采纳答案by óscar López
Try this for Python 2.x:
在 Python 2.x 上试试这个:
range(1,6) + range(15,20)
Or if you're using Python3.x, try this:
或者,如果您使用的是 Python3.x,请尝试以下操作:
list(range(1,6)) + list(range(15,20))
For dealing with elements in-between, for Python 2.x:
对于处理中间元素,对于 Python 2.x:
range(101,6284) + [8001,8003,8010] + range(10000,12322)
And finally for dealing with elements in-between, for Python 3.x:
最后是处理中间元素,对于 Python 3.x:
list(range(101,6284)) + [8001,8003,8010] + list(range(10000,12322))
The key aspects to remember here is that in Python 2.x rangereturns a list and in Python 3.x it returns an iterable (so it needs to be explicitly converted to a list). And that for appending together lists, you can use the +operator.
这里要记住的关键方面是,在 Python 2.x 中range返回一个列表,而在 Python 3.x 中它返回一个可迭代对象(因此需要显式转换为列表)。对于将列表附加在一起,您可以使用+运算符。
回答by Silas Ray
rangereturns a list to begin with, so you need to either concatenate them together with +or use append()or extend().
range返回一个列表以开始,因此您需要将它们连接在一起+或使用append()or extend()。
new_list = range(1,6) + range(15,20)
or
或者
new_list = range(101,6284)
mew_list.extend([8001,8003,8010])
mew_list.extend(range(10000,12322))
Alternatively, you could use itertools.chain()as shown in Shawn Chin's answer.
或者,您可以itertools.chain()按照 Shawn Chin 的回答使用。
回答by Shawn Chin
You can use itertools.chainto flatten the output of your range()calls.
您可以使用itertools.chain来展平您的range()调用输出。
import itertools
new_list = list(itertools.chain(xrange(1,6), xrange(15,20)))
Using xrange(or simply range()for python3) to get an iterable and chaining them together means only one list object gets created (no intermediate lists required).
使用xrange(或仅range()用于 python3)获取可迭代对象并将它们链接在一起意味着只创建一个列表对象(不需要中间列表)。
If you need to insert intermediate values, just include a list/tuple in the chain:
如果您需要插入中间值,只需在链中包含一个列表/元组:
new_list = list(itertools.chain((-3,-1),
xrange(1,6),
tuple(7), # make single element iterable
xrange(15,20)))
回答by Hymancogdill
Try this:
尝试这个:
from itertools import chain
new_list = [x for x in chain(range(1,6), range(15,20))]
print new_list
Output like you wanted:
像你想要的那样输出:
[1, 2, 3, 4, 5, 15, 16, 17, 18, 19]
回答by Mikhail
i would like to propose u a version without +
我想建议没有 + 的 ua 版本
import operator
a = list(range(1,6))
b = list(range(7,9))
print(operator.concat(a,b))

