Dictionary slices in Julia

834 views
Skip to first unread message

Leif Jonsson

unread,
Nov 10, 2013, 6:09:51 AM11/10/13
to julia...@googlegroups.com
Hi, 

Is it possible to extract dictionary slices in Julia?
What I have in mind is something along the lines of:

julia> mydict = {"A"=>3, "B"=>5, "C"=>1}

{"B"=>5,"C"=>1,"A"=>3}


julia> mydict["B","C"]

ERROR: no method getindex(Dict{Any,Any},ASCIIString,ASCIIString)


But without getting an error? :) 


Best Regards

-Leif


David van Leeuwen

unread,
Nov 10, 2013, 7:43:37 AM11/10/13
to julia...@googlegroups.com
Hello, 

I think you would want to say 

mydict[["A","B"]]



but for that you need to define an additional getindex()

getindex(d::Dict, a::Array) = [d[i] for i=a]




I would think it made sense if this was standard for Dict. 

Cheers, 

---david

Stefan Karpinski

unread,
Nov 10, 2013, 10:38:50 AM11/10/13
to Julia Users
You can't do that because arrays can be keys in dicts. I'm sympathetic to the idea of dictionary slices, but I'm not sure we can actually support it because of this. The dictionary comprehension syntax is quite compact.

Kevin Squire

unread,
Nov 10, 2013, 11:38:10 AM11/10/13
to julia...@googlegroups.com
On the other hand, the original syntax could be supported:

julia> mydict = {"A"=>3, "B"=>5, "C"=>1}
{"B"=>5,"C"=>1,"A"=>3}


julia
> import Base.getindex

julia
> getindex(d::Dict, xs...) = [getindex(d, x) for x in xs]
getindex
(generic function with 140 methods)


julia
> mydict["B","C"]
2-element Array{Any,1}:
 
5
 
1

Whether this is a good idea or not is unclear to me.

Kevin
Message has been deleted

Leif Jonsson

unread,
Nov 13, 2013, 2:52:31 AM11/13/13
to julia...@googlegroups.com
Hmmm, I posted a reply to this but it seems to have gone to /dev/null ...

*nod* I suspected as much.

I'm new to Julia so forgive my ignorance, but if you would allow me a couple of follow up questions:

   1. Would it be possible to introduce a specific syntax for Dictionary slices? Something along the lines of: mydict<"B","C"> => [5, 1] or some alternative.
 2. If not, would it be worth removing the possibilities of having specifically Arrays as Dict keys? I have never used it myself, but perhaps there is a common use-case for Arrays as hash keys that I am missing?

Best Regards
-Leif Jonsson

Leif Jonsson

unread,
Nov 13, 2013, 2:53:48 AM11/13/13
to julia...@googlegroups.com
Sorry, that should probably be mydict<["B","C"]> => [5, 1]

Best, 
-Leif

Kevin Squire

unread,
Nov 13, 2013, 2:20:32 PM11/13/13
to julia...@googlegroups.com
While I'm not the final arbiter, I'm guessing that adding new syntax won't fly.  However, with the syntax I proposed, splatting would work fine:

julia> import Base.getindex

julia
> getindex(d::Dict, xs...) = [getindex(d, x) for x in xs]
getindex
(generic function with 140 methods)


julia
> mydict = {"A"=>3, "B"=>5, "C"=>1}

{"B"=>5,"C"=>1,"A"=>3}


julia
> mydict[["B","C"]...]

2-element Array{Any,1}:
 
5
 
1


julia
> c = ["B","C"];

julia
> mydict[c...]

2-element Array{Any,1}:
 
5
 
1

I think this is worthwhile and useful, so I'm going to submit it as a pull request.  It won't be part of v0.2, but if there aren't any objections, it will be available in future versions (or when building from source).  In the mean time, if this is useful for you, you could make it available for your own personal use by adding these two lines to your .juliarc file:

import Base.getindex
getindex(d::Associative, xs...) = [getindex(d, x) for x in xs]

Kevin

Kevin Squire

unread,
Nov 13, 2013, 2:38:03 PM11/13/13
to julia...@googlegroups.com
BTW, to get better dictionary types, it is probably better to create your dictionaries with square brackets instead of braces:

julia> import Base.getindex

julia
> getindex(d::Dict, xs...) = [getindex(d, x) for x in xs]
getindex
(generic function with 140 methods)


julia
> mydict = {"A"=>3, "B"=>5, "C"=>1}

{"B"=>5,"C"=>1,"A"=>3}


julia
> mydict[["B","C"]...]

2-element Array{Any,1}:
 
5
 
1


julia
> c = ["B","C"];

julia
> mydict[c...]

2-element Array{Any,1}:
 
5
 
1

Curly braces always create an (Any=>Any) dictionary, which can sometimes be useful, but for better performance, often you'll want a specific type for the keys/values.

Kevin

Kevin Squire

unread,
Nov 13, 2013, 2:39:11 PM11/13/13
to julia...@googlegroups.com
Sorry, copy and paste error.  The correct example is below:

julia> mydict1 = {"A"=>3, "B"=>5, "C"=>1}
{"B"=>5,"C"=>1,"A"=>3}

julia> typeof(mydict1)
Dict{Any,Any} (constructor with 2 methods)

julia> mydict2 = ["A"=>3, "B"=>5, "C"=>1]
["B"=>5,"C"=>1,"A"=>3]

julia> typeof(mydict2)
Dict{ASCIIString,Int64} (constructor with 2 methods)

julia> mydict3 = (ASCIIString=>Int64)[]
Dict{ASCIIString,Int64}()


Kevin

Kevin Squire

unread,
Nov 13, 2013, 2:40:28 PM11/13/13
to julia...@googlegroups.com

Leif Jonsson

unread,
Nov 14, 2013, 5:24:03 AM11/14/13
to julia...@googlegroups.com
Nice!! :)

No, I don't care (much :) ) about the syntax, but I do very much like the functionality! :)

Cheers!
-Leif
Reply all
Reply to author
Forward
0 new messages