可以一次附加多个列表吗? (Python) [英] Possible to append multiple lists at once? (Python)

查看:89
本文介绍了可以一次附加多个列表吗? (Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆列表,我想附加到一个列表中,这是我要编写的程序中的主"列表.有没有办法在一行代码中而不是在10行中做到这一点?我是一个初学者,所以我不知道...

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...

为了更好地了解我的问题,如果我有以下列表,该怎么办:

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]

并且想要将y和z附加到x.而不是这样做:

And want to append y and z to x. Instead of doing:

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)

它不会工作.

推荐答案

x.extend(y+z)

应该做你想做的

x += y+z

甚至

x = x+y+z

这篇关于可以一次附加多个列表吗? (Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆