问题
How do I repeat each element of a list n times and form a new list? For example:
x=[1,2,3,4]
n=3
Looking for:
[1,1,1,2,2,2,3,3,3,4,4,4]
回答1:
An inner
argument to repeat is what I was looking for:
repeat([1, 2, 3, 4], inner = 3)
回答2:
Also list comprehension:
x = [1,2,3,4]
n = 3
result = [i for i in x for j in 1:n]
来源:https://stackoverflow.com/questions/60234868/how-to-repeat-elements-in-list-n-times