📅  最后修改于: 2023-12-03 15:24:37.236000             🧑  作者: Mango
在Python中,我们可以使用多种方式来复制一个列表中的项目n次。以下是一些示例:
lst = [1, 2, 3]
n = 3
new_lst = []
for item in lst:
for i in range(n):
new_lst.append(item)
print(new_lst) # Output: [1, 1, 1, 2, 2, 2, 3, 3, 3]
lst = [1, 2, 3]
n = 3
new_lst = [item for item in lst for i in range(n)]
print(new_lst) # Output: [1, 1, 1, 2, 2, 2, 3, 3, 3]
lst = [1, 2, 3]
n = 3
new_lst = lst * n
print(new_lst) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
以上是三种常见的方法,根据实际场景选择不同的方法来复制列表中的项目n次。