I have a bunch of lists I want to append to a single list that is sort of the "main" list in a program I'm trying to write. Is there a way to do this in one line of code rather than like 10? I'm a beginner so I have no idea...
我有一堆列表要添加到单个列表中,这个列表是我正在尝试编写的程序中的“主要”列表。有没有办法在一行代码而不是10代码中执行此操作?我是初学者,所以我不知道......
For a better picture of my question, what if I had these lists:
为了更好地了解我的问题,如果我有这些列表怎么办:
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
And want to append y and z to x. Instead of doing:
并希望将y和z附加到x。而不是做:
x.append(y)
x.append(z)
Is there a way to do this in one line of code? I already tried:
有没有办法在一行代码中执行此操作?我已经尝试过:
x.append(y, z)
And it wont work.
它不会工作。
6 个解决方案
#1
28
x.extend(y+z)
should do what you want
应该做你想做的事
or
要么
x += y+z
or even
甚至
x = x+y+z
#2
14
Extending my comment
扩展我的评论
In [1]: x = [1, 2, 3]
In [2]: y = [4, 5, 6]
In [3]: z = [7, 8, 9]
In [4]: from itertools import chain
In [5]: print list(chain(x,y,z))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#3
5
You can use sum
function with start value (empty list) indicated:
您可以使用sum函数和指示的起始值(空列表):
a = sum([x, y, z], [])
This is especially more suitable if you want to append an arbitrary number of lists.
如果要附加任意数量的列表,这尤其适用。
#4
0
equivalent to above answer, but sufficiently different to be worth a mention:
相当于上面的答案,但足够不同值得一提:
my_map = {
'foo': ['a', 1, 2],
'bar': ['b', '2', 'c'],
'baz': ['d', 'e', 'f'],
}
list(itertools.chain(*my_map.values()))
['d', 'e', 'f', 'a', 1, 2, 'b', '2', 'c']
in the above expression, * is important for groking result as args to chain, this is same as prior chain(x,y,z). Also, note the result is hash-ordered.
在上面的表达式中,*对于将结果作为args转换为链是重要的,这与先前的链(x,y,z)相同。另外,请注意结果是哈希排序的。
#5
0
If you prefer a slightly more functional approach, you could try:
如果您更喜欢功能稍强的方法,可以尝试:
import functools as f
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
x = f.reduce(lambda x, y: x+y, [y, z], x)
This will enable you to concatenate any number of lists onto list x
.
这将使您能够将任意数量的列表连接到列表x上。
If you would just like to concatenate any number of lists together (i.e. not onto some base list), you can simplify to:
如果您只想将任意数量的列表连接在一起(即不在某些基本列表中),您可以简化为:
import functools as f
from operator import add
big_list = f.reduce(add, list_of_lists)
Take note that our BFDL has his reservations with regard to lambdas, reduce, and friends: https://www.artima.com/weblogs/viewpost.jsp?thread=98196
请注意,我们的BFDL对lambdas,reduce和朋友有所保留:https://www.artima.com/weblogs/viewpost.jsp?thread = 98196
To complete this answer, you can read more about reduce in the documentation: https://docs.python.org/3/library/functools.html#functools.reduce
要完成此答案,您可以在文档中阅读有关reduce的更多信息:https://docs.python.org/3/library/functools.html#functools.reduce
I quote: "Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value."
我引用:“将两个参数的函数累加到序列项中,从左到右,以便将序列减少为单个值。”
P.S. Using sum()
as described in https://*.com/a/41752487/532513 is super compact, it does seem to work with lists, and is really fast (see https://*.com/a/33277438/532513 ) but help(sum)
in Python 3.6 has the following to say:
附:使用https://*.com/a/41752487/532513中描述的sum()非常紧凑,它似乎与列表一起使用,并且非常快(请参阅https://*.com/a/33277438/ 532513)但Python 3.6中的帮助(总和)有以下说法:
This function is intended specifically for use with numeric values and may reject non-numeric types.
此函数专门用于数值,可能会拒绝非数字类型。
Although this is slightly worrying, I will probably keep it as my first option for concatenating lists.
虽然这有点令人担忧,但我可能会将它作为连接列表的第一选择。
#6
0
In one line , it can be done in following ways
在一行中,可以通过以下方式完成
x.extend(y+z)
or
要么
x=x+y+z
#1
28
x.extend(y+z)
should do what you want
应该做你想做的事
or
要么
x += y+z
or even
甚至
x = x+y+z
#2
14
Extending my comment
扩展我的评论
In [1]: x = [1, 2, 3]
In [2]: y = [4, 5, 6]
In [3]: z = [7, 8, 9]
In [4]: from itertools import chain
In [5]: print list(chain(x,y,z))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
#3
5
You can use sum
function with start value (empty list) indicated:
您可以使用sum函数和指示的起始值(空列表):
a = sum([x, y, z], [])
This is especially more suitable if you want to append an arbitrary number of lists.
如果要附加任意数量的列表,这尤其适用。
#4
0
equivalent to above answer, but sufficiently different to be worth a mention:
相当于上面的答案,但足够不同值得一提:
my_map = {
'foo': ['a', 1, 2],
'bar': ['b', '2', 'c'],
'baz': ['d', 'e', 'f'],
}
list(itertools.chain(*my_map.values()))
['d', 'e', 'f', 'a', 1, 2, 'b', '2', 'c']
in the above expression, * is important for groking result as args to chain, this is same as prior chain(x,y,z). Also, note the result is hash-ordered.
在上面的表达式中,*对于将结果作为args转换为链是重要的,这与先前的链(x,y,z)相同。另外,请注意结果是哈希排序的。
#5
0
If you prefer a slightly more functional approach, you could try:
如果您更喜欢功能稍强的方法,可以尝试:
import functools as f
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
x = f.reduce(lambda x, y: x+y, [y, z], x)
This will enable you to concatenate any number of lists onto list x
.
这将使您能够将任意数量的列表连接到列表x上。
If you would just like to concatenate any number of lists together (i.e. not onto some base list), you can simplify to:
如果您只想将任意数量的列表连接在一起(即不在某些基本列表中),您可以简化为:
import functools as f
from operator import add
big_list = f.reduce(add, list_of_lists)
Take note that our BFDL has his reservations with regard to lambdas, reduce, and friends: https://www.artima.com/weblogs/viewpost.jsp?thread=98196
请注意,我们的BFDL对lambdas,reduce和朋友有所保留:https://www.artima.com/weblogs/viewpost.jsp?thread = 98196
To complete this answer, you can read more about reduce in the documentation: https://docs.python.org/3/library/functools.html#functools.reduce
要完成此答案,您可以在文档中阅读有关reduce的更多信息:https://docs.python.org/3/library/functools.html#functools.reduce
I quote: "Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value."
我引用:“将两个参数的函数累加到序列项中,从左到右,以便将序列减少为单个值。”
P.S. Using sum()
as described in https://*.com/a/41752487/532513 is super compact, it does seem to work with lists, and is really fast (see https://*.com/a/33277438/532513 ) but help(sum)
in Python 3.6 has the following to say:
附:使用https://*.com/a/41752487/532513中描述的sum()非常紧凑,它似乎与列表一起使用,并且非常快(请参阅https://*.com/a/33277438/ 532513)但Python 3.6中的帮助(总和)有以下说法:
This function is intended specifically for use with numeric values and may reject non-numeric types.
此函数专门用于数值,可能会拒绝非数字类型。
Although this is slightly worrying, I will probably keep it as my first option for concatenating lists.
虽然这有点令人担忧,但我可能会将它作为连接列表的第一选择。
#6
0
In one line , it can be done in following ways
在一行中,可以通过以下方式完成
x.extend(y+z)
or
要么
x=x+y+z