期望输入:
list_before = [1, 2, 3, 4, 5]
期望输出:
list_after = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
期望实现的功能是让列表中元素的排列顺序不变,其中每个元素重复相同的次数。
最终代码实现:
list_before = [1, 2, 3, 4, 5]
list_after = [val for val in list_before for i in range(2)]
list_before = [1, 2, 3, 4, 5]
list_after = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
list_before = [1, 2, 3, 4, 5]
list_after = [val for val in list_before for i in range(2)]