Thanks @Tomas. That is a pretty cool solution.
I just figured out another solution that does not require tuple order
inversion. I was pleasantly surprised to to discover that Julia's "list"
comprehension be used to build dictionaries.
Here is my solution:
julia> t = ((1, 'a'),(2, 'b'))
((1,'a'),(2,'b'))
julia> [b=>a for (a,b) in t]
{'a'=>1,'b'=>2}
On Monday, May 26, 2014 1:37:12 PM UTC+3, Tomas Lycken wrote:
>
> The easiest way I could find was requires you to invert the order of your
> tuples. But it turns out in an even more logical way, IMO:
>
> julia> t = (("a", 1), ("b", 2))
> (("a",1),("b",2))
> julia> Dict(zip(t...)...)
> ["b"=>2,"a"=>1]
>
> zip <http://docs.julialang.org/en/latest/stdlib/base/#Base.zip> is one of
> my favourite functions, and it should be a familiar concept coming from
> python.
>
> // T
> On Monday, May 26, 2014 11:34:55 AM UTC+2, Mohammed El-Beltagy wrote:
>>
>> In python I could easily generate a dictionary from a list of tuples
>>
>> For example
>>
>> >>> t = ((1, 'a'),(2, 'b'))
>> >>> dict((y, x) for x, y in t)
>> {'a': 1, 'b': 2}
>>
>>
>> Is is possible to do something similar in Julia?
>>
>> I tried the map function but it gave me errors
>> julia> Dict(map(k->(k[2],k[1]),t))
>>
>> no method Dict{K,V}(Array{Any,1},)
>> at In[100]:1
>>
>>
>>
>>