Is there an equivalent to Python's pop? I have an array x and a boolean array flag of the same length. I would like to extract x[flag] and be able to store it in a variable x_flagged while at the same time remove them in place from x.

x = rand(1:5, 100) flag = x .> 2 x_flagged = some_function!(x, flag) # Now x would be equal to x[x .<= 2] 
1

2 Answers

Try this one using deleteat!

julia> function pop_r!(list, y) t = list[y]; deleteat!( list, y ); t end julia> x = rand(1:5, 100) 100-element Vector{Int64} julia> flag = x .> 2 100-element BitVector julia> pop_r!( x, flag ) 60-element Vector{Int64} julia> x 40-element Vector{Int64} 

You can use splice! with a bit of help from findall:

julia> x_flagged = splice!(x, findall(flag)) 59-element Vector{Int64}: ... julia> size(x) (41,) 

splice!(a::Vector, indices, [replacement]) -> items
Remove items at specified indices, and return a collection containing the removed items.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.