Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete element in an array for julia

Tags:

I've been wandering for a while in the docs and in forums and I haven't found a built in method/function to do the simple task of deleting an element in an array. Is there such built-in function?

I am asking for the equivalent of python's list.remove(x).

Here's an example of naively picking a function from the box:

julia> a=Any["D","A","s","t"] julia> pop!(a, "s") ERROR: MethodError: `pop!` has no method matching        pop!(::Array{Any,1},     ::ASCIIString) Closest candidates are:   pop!(::Array{T,1})   pop!(::ObjectIdDict, ::ANY, ::ANY)   pop!(::ObjectIdDict, ::ANY)   ... 

Here mentions to use deleteat!, but also doesn't work:

julia> deleteat!(a, "s") ERROR: MethodError: `-` has no method matching -(::Int64, ::Char) Closest candidates are:   -(::Int64)   -(::Int64, ::Int64)   -(::Real, ::Complex{T<:Real})   ...   in deleteat! at array.jl:621 
like image 913
user2820579 Avatar asked Feb 09 '16 17:02

user2820579


People also ask

How do you delete an element from an array in Ruby?

Ruby | Array delete() operation Array#delete() : delete() is a Array class method which returns the array after deleting the mentioned elements. It can also delete a particular element in the array. Syntax: Array. delete() Parameter: obj - specific element to delete Return: last deleted values from the array.

How do I delete a variable in Julia?

In Julia 0.6. You can remove the variable and free up it's memory by calling clear!() .

How do you remove an element from an array with value?

We can use the following JavaScript methods to remove an array element by its value. indexOf() – function is used to find array index number of given value. Return negavie number if the matching element not found. splice() function is used to delete a particular index value and return updated array.


1 Answers

You can also go with filter!:

a = Any["D", "A", "s", "t"] filter!(e->e≠"s",a) println(a) 

gives:

Any["D","A","t"] 

This allows to delete several values at once, as in:

filter!(e->e∉["s","A"],a) 

Note 1: In Julia 0.5, anonymous functions are much faster and the little penalty felt in 0.4 is not an issue anymore :-) .

Note 2: Code above uses unicode operators. With normal operators: is != and e∉[a,b] is !(e in [a,b])

like image 177
Dan Getz Avatar answered Sep 20 '22 06:09

Dan Getz