首页 > 解决方案 > 复制列表中的对象以使其重复

问题描述

如何复制列表中的元素以使它们重复?

Input: ListA = [1,2,3,4,5,6,7,8,9]

Output: ListA = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9]

标签: pythonpandas

解决方案


我们可以用np.repeat()

ListA = [1,2,3,4,5,6,7,8,9]
ListA = np.repeat([1,2,3,4,5,6,7,8,9], 2)
ListA

输出

array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9])

推荐阅读