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

487 views
Skip to first unread message

Fred

unread,
Nov 7, 2016, 9:02:44 AM11/7/16
to julia-users
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 !

Steven G. Johnson

unread,
Nov 7, 2016, 9:18:03 AM11/7/16
to julia-users


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]

Fred

unread,
Nov 7, 2016, 9:27:33 AM11/7/16
to julia-users
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




Mauro

unread,
Nov 7, 2016, 9:45:37 AM11/7/16
to julia...@googlegroups.com
On Mon, 2016-11-07 at 15:27, Fred <fred.so...@gmail.com> 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.

Fred

unread,
Nov 7, 2016, 9:49:50 AM11/7/16
to julia-users
Thank you very much Mauro, that was the mistake ! :)

Steven G. Johnson

unread,
Nov 7, 2016, 10:27:49 AM11/7/16
to julia-users


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

Array{String}(0)

Whoops, sorry about the typo. 

Dan

unread,
Nov 8, 2016, 2:39:53 AM11/8/16
to julia-users
a = Vector{String}



is also a good option.

Dan

unread,
Nov 8, 2016, 2:40:37 AM11/8/16
to julia-users
I meant,

a = Vector{String}()

Dan

Fred

unread,
Nov 8, 2016, 7:47:26 AM11/8/16
to julia-users
Thank you Dan, this solution works to ! I don't know which one is better :)

Fabian Gans

unread,
Nov 9, 2016, 4:00:49 AM11/9/16
to julia-users
I usually use

= String[]

Fabian

David P. Sanders

unread,
Nov 9, 2016, 9:47:27 AM11/9/16
to julia-users
An alternative syntax is

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

which iterates over (key, value) pairs in the dictionary.
 
Reply all
Reply to author
Forward
0 new messages