Quantcast

How to built an array with some keys of a dictionary ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to built an array with some keys of a dictionary ?

Fred
Hi,

I have many problems to build arrays with the keys of a dictionary except for the simple situation :

a = [uppercase(key) for key in keys(dict)]    # works fine

If I try to select the keys with more complex criteria like :

dict = Dict("a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5)

a
= Array{String}

for k in keys(dict)
 
if dict[k] < 2
   
continue
 
end
    push
!(a, k) # building array from keys produces errors
end


ERROR
: MethodError: `push!` has no method matching push!(::Type{Array{AbstractString,N}}, ::ASCIIString)




Thank you for your help !
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to built an array with some keys of a dictionary ?

Steven G. Johnson


On Monday, November 7, 2016 at 9:02:44 AM UTC-5, Fred wrote:
Hi,

I have many problems to build arrays with the keys of a dictionary except for the simple situation :

a = [uppercase(key) for key in keys(dict)]    # works fine

If I try to select the keys with more complex criteria like :

dict = Dict("a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5)

a
= Array{String}

This is a mistake: you just assigned "a" to an array type, not an array instance.  It should be a = Array{String}(). 

In Julia 0.5, you can also just do:

[k for k in keys(dict) if dict[k] < 2]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to built an array with some keys of a dictionary ?

Fred
Hi Steven,


I also tried  a = Array{String}() unfortunately it produces errors as well.



julia
> a = Array{String}()
WARNING
: Base.String is deprecated, use AbstractString instead.
  likely near
no file:0
0-dimensional Array{AbstractString,0}:
#undef


julia
> for k in keys(dict)
         
if dict[k] < 2
           
continue
         
end
           push
!(a, k)
       
end
ERROR
: MethodError: `push!` has no method matching push!(::Array{AbstractString,0}, ::ASCIIString)
Closest candidates are:
  push
!(::Any, ::Any, ::Any)
  push
!(::Any, ::Any, ::Any, ::Any...)
  push
!(::Array{Any,1}, ::ANY)
 
...
 
[inlined code] from none:5
 
in anonymous at no file:0




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to built an array with some keys of a dictionary ?

Mauro
On Mon, 2016-11-07 at 15:27, Fred <[hidden email]> wrote:
> Hi Steven,
>
>
> I also tried a = Array{String}() unfortunately it produces errors as well.

Array{String}(0)

works.  The other creates a zero-dimensional vector (just like a scalar)
to which you cannot push.

> julia> a = Array{String}()
> WARNING: Base.String is deprecated, use AbstractString instead.
>  likely near no file:0
> 0-dimensional Array{AbstractString,0}:
> #undef
>
>
> julia> for k in keys(dict)
>     if dict[k] < 2
>      continue
>     end
>      push!(a, k)
>    end
> ERROR: MethodError: `push!` has no method matching push!(::Array{AbstractString
> ,0}, ::ASCIIString)
> Closest candidates are:
>  push!(::Any, ::Any, ::Any)
>  push!(::Any, ::Any, ::Any, ::Any...)
>  push!(::Array{Any,1}, ::ANY)
>  ...
> [inlined code] from none:5
> in anonymous at no file:0
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to built an array with some keys of a dictionary ?

Fred
Thank you very much Mauro, that was the mistake ! :)


Array{String}(0)

works.  The other creates a zero-dimensional vector (just like a scalar)
to which you cannot push.

> julia> a = Array{String}()
> WARNING: Base.String is deprecated, use AbstractString instead.
>  likely near no file:0
> 0-dimensional Array{AbstractString,0}:
> #undef
>
>
> julia> for k in keys(dict)
>     if dict[k] < 2
>      continue
>     end
>      push!(a, k)
>    end
> ERROR: MethodError: `push!` has no method matching push!(::Array{AbstractString
> ,0}, ::ASCIIString)
> Closest candidates are:
>  push!(::Any, ::Any, ::Any)
>  push!(::Any, ::Any, ::Any, ::Any...)
>  push!(::Array{Any,1}, ::ANY)
>  ...
> [inlined code] from none:5
> in anonymous at no file:0
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to built an array with some keys of a dictionary ?

Steven G. Johnson
In reply to this post by Mauro


On Monday, November 7, 2016 at 9:45:37 AM UTC-5, Mauro wrote:
On Mon, 2016-11-07 at 15:27, Fred <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="6HL3xn1vBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fred.so...@...> wrote:
> Hi Steven,
>
>
> I also tried a = Array{String}() unfortunately it produces errors as well.

Array{String}(0)

Whoops, sorry about the typo. 
Dan
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to built an array with some keys of a dictionary ?

Dan
a = Vector{String}



is also a good option.

On Monday, November 7, 2016 at 5:27:49 PM UTC+2, Steven G. Johnson wrote:


On Monday, November 7, 2016 at 9:45:37 AM UTC-5, Mauro wrote:
On Mon, 2016-11-07 at 15:27, Fred <[hidden email]> wrote:
> Hi Steven,
>
>
> I also tried a = Array{String}() unfortunately it produces errors as well.

Array{String}(0)

Whoops, sorry about the typo. 
Dan
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to built an array with some keys of a dictionary ?

Dan
I meant,

a = Vector{String}()

Dan

On Tuesday, November 8, 2016 at 9:39:53 AM UTC+2, Dan wrote:
a = Vector{String}



is also a good option.

On Monday, November 7, 2016 at 5:27:49 PM UTC+2, Steven G. Johnson wrote:


On Monday, November 7, 2016 at 9:45:37 AM UTC-5, Mauro wrote:
On Mon, 2016-11-07 at 15:27, Fred <[hidden email]> wrote:
> Hi Steven,
>
>
> I also tried a = Array{String}() unfortunately it produces errors as well.

Array{String}(0)

Whoops, sorry about the typo. 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to built an array with some keys of a dictionary ?

Fred
Thank you Dan, this solution works to ! I don't know which one is better :)

Le mardi 8 novembre 2016 08:40:37 UTC+1, Dan a écrit :
I meant,

a = Vector{String}()

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to built an array with some keys of a dictionary ?

Fabian Gans
In reply to this post by Dan
I usually use

= String[]

Fabian
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to built an array with some keys of a dictionary ?

David P. Sanders
In reply to this post by Steven G. Johnson


El lunes, 7 de noviembre de 2016, 9:18:03 (UTC-5), Steven G. Johnson escribió:


On Monday, November 7, 2016 at 9:02:44 AM UTC-5, Fred wrote:
Hi,

I have many problems to build arrays with the keys of a dictionary except for the simple situation :

a = [uppercase(key) for key in keys(dict)]    # works fine

If I try to select the keys with more complex criteria like :

dict = Dict("a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5)

a
= Array{String}

This is a mistake: you just assigned "a" to an array type, not an array instance.  It should be a = Array{String}(). 

In Julia 0.5, you can also just do:

[k for k in keys(dict) if dict[k] < 2]

An alternative syntax is

[k for (k,v) in dict if v < 2]

which iterates over (key, value) pairs in the dictionary.
 
Loading...