How to repeat elements in list n times?

微笑、不失礼 提交于 2021-02-05 06:29:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!