简体   繁体   English

在 python 中附加重复列表

[英]Appending a repeated list in python

I have a list here:我在这里有一个清单:

x = [['a', 'b']]

y = ['c', 'd', 'e']

I want to append the list y to make x in the form我想 append 列表y使x的形式

x = [['a', 'b'], ['c', 'd', 'e'], ['c', 'd', 'e'], ['c', 'd', 'e']]

x.append(y*3) makes x = [['a', 'b'], ['c', 'd', 'e', 'c', 'd', 'e', 'c', 'd', 'e']] x.append(y*3)使得x = [['a', 'b'], ['c', 'd', 'e', 'c', 'd', 'e', 'c', 'd', 'e']]

x.append([y]*3) makes x = [['a', 'b'], [['c', 'd', 'e'], ['c', 'd', 'e'], ['c', 'd', 'e']]] x.append([y]*3)使得x = [['a', 'b'], [['c', 'd', 'e'], ['c', 'd', 'e'], ['c', 'd', 'e']]]

How can I make the list [['a', 'b'], ['c', 'd', 'e'], ['c', 'd', 'e'], ['c', 'd', 'e']] from x and y ?如何制作列表[['a', 'b'], ['c', 'd', 'e'], ['c', 'd', 'e'], ['c', 'd', 'e']]来自xy

try this:尝试这个:

x = [['a', 'b']]

y = ['c', 'd', 'e']

x += [y] * 3

print(x)

This will output:这将 output:

[['a', 'b'], ['c', 'd', 'e'], ['c', 'd', 'e'], ['c', 'd', 'e']]
x = [['a', 'b']]
y = ['c', 'd', 'e']
x=x+[y]+[y]+[y]
print(x)

output:
[['a', 'b'], ['c', 'd', 'e'], ['c', 'd', 'e'], ['c', 'd', 'e']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM