可以一次附加多个列表吗?(Python)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14131615/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:33:16  来源:igfitidea点击:

Possible to append multiple lists at once? (Python)

pythonlistappend

提问by anon

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.

它不会工作。

采纳答案by Joran Beasley

x.extend(y+z)

should do what you want

应该做你想做的

or

或者

x += y+z

or even

甚至

x = x+y+z

回答by night-crawler

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]

回答by msudder

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.

在上面的表达式中, * 对于 groking 结果很重要,因为 args 链接,这与先前的链(x,y,z)相同。另请注意,结果是按哈希排序的。

回答by Minjoon Seo

You can use sumfunction 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.

如果您想附加任意数量的列表,这尤其适合。

回答by Charl Botha

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.

这将使您能够将任意数量的列表连接到 list 上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://stackoverflow.com/a/41752487/532513is super compact, it does seem to work with lists, and is really fast (see https://stackoverflow.com/a/33277438/532513) but help(sum)in Python 3.6 has the following to say:

PSsum()按照https://stackoverflow.com/a/41752487/532513中的描述使用非常紧凑,它似乎可以处理列表,而且速度非常快(参见https://stackoverflow.com/a/33277438/532513)但help(sum)在 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.

尽管这有点令人担忧,但我可能会将其作为连接列表的第一个选项。

回答by SomnAth Pal

In one line , it can be done in following ways

在一行中,可以通过以下方式完成

x.extend(y+z)

or

或者

x=x+y+z

回答by jillm_5

To exactly replicate the effect of append, try the following function, simple and effective:

要准确复制append的效果,试试下面的函数,简单有效:

a=[]
def concats (lists):
    for i in lists:
        a==a.append(i)


concats ([x,y,z])
print(a)