List challenge

I have a list: my_list=collect(1:n)', and a repeatability vector: repeat_list = [n1, n1, n2, n1, n2, n2]. I would like to create a new list with repeated n1 times, 2 repeated n1 times, 3 repeated n2 times, etc. The length of the list could be a few hundred thousand. Is there a known “best” way to accomplish this? In the meantime, I keep searching. Thanks.

reduce(append!, Iterators.repeated.(eachindex(repeat_list), repeat_list), init=Int[])?

2 Likes

Something like this?

[fill(a,n) for (a,n) in zip(my_list, repeat_list)]

(I did not test is, but will do tomorrow :smile: )

What you are referring to is run-length encoding. Or rather, what you start out with is run-length encoding, and you want to do inverse run-length encoding. The StatsBase.jl package has an rle function and an inverse_rle function. The inverse_rle function does what you’re looking for:

julia> inverse_rle([5, 3, 1], [1, 2, 3])
6-element Array{Int64,1}:
 5
 3
 3
 1
 1
 1
6 Likes

Thank you all for your answers and ideas, which I will try out. I can now tell you that the case I was actually working with (for now) involved only two numbers separated by unity. Thus, given a sequence: seq=collect(1:100) along with a sequence seq2 with a mixture of 5 and 6’s? I wanted to create a new sequence with the numbers in seq repeated according to the information in seq2. This was easily accomplished with two conditional statement and hcat. I wonder how efficient my implementation is compared to the ideas offered above. Thanks again! I have learned a lot!

This should be foldl rather than reduce, since the operation is not associative. The fact that reduce happens to work in this case is due to an implementation detail (that by default, reduce is implemented as foldl)

1 Like

Agreed.
Still, I am amused by how nicely the return values of push! and append! fit together with the fold concept.

1 Like