Julia - 相当于 python `pop`。使用布尔数组和 return 从数组中删除元素
Julia - Equivalent of python `pop`. Remove elements from array using boolean array and return them
有没有等同于 Python 的 pop
?我有一个长度相同的数组 x
和一个布尔数组 flag
。我想提取 x[flag]
并能够将其存储在变量 x_flagged
中,同时将它们从 x
.
中删除
x = rand(1:5, 100)
flag = x .> 2
x_flagged = some_function!(x, flag) # Now x would be equal to x[x .<= 2]
使用 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}
您可以在 findall
的帮助下使用 splice!
:
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.
有没有等同于 Python 的 pop
?我有一个长度相同的数组 x
和一个布尔数组 flag
。我想提取 x[flag]
并能够将其存储在变量 x_flagged
中,同时将它们从 x
.
x = rand(1:5, 100)
flag = x .> 2
x_flagged = some_function!(x, flag) # Now x would be equal to x[x .<= 2]
使用 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}
您可以在 findall
的帮助下使用 splice!
:
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.