如何在Python中同时追加项目多次?

时间:2012-03-20 17:44:16

标签: python list append

我想追加一个项目不止一次。 e.g

listA = ['AS','23','45']
listB = ['TH','67','78']

listB.append(listA.pop()*3)

print(listA)
# ['AS', '23']

print(listB)
#['TH', '67', '78', '454545']

在打印listB上,它目前给我上面的列表 但我想让它给我# ['TH', '67', '78', '45','45','45']而不是

我该怎么做。

1 个答案:

答案 0 :(得分:7)

尝试使用list.extend()并重复pop()返回的字符串,而不是单个元素列表:

>>> listA = ['AS','23','45']
>>> listB = ['TH','67','78']
>>> listB.extend([listA.pop()]*3)
>>> listB
['TH', '67', '78', '45', '45', '45']