Re: [julia-users] Converting vectors to dictionaries back and forth

2015-09-01 Thread Yichao Yu
On Tue, Sep 1, 2015 at 9:39 AM,   wrote:
> Hi,
>
> I want to be able to convert vectors to dictionaries and the other way
> round. I have coded these two functions:
>
> function vec_to_dict(v, ll)
> d = Dict()
> assert(length(v) == length(ll))
> for i = 1:length(v)
> d[ll[i]] = v[i]
> end
> return d
> end
>
> function dict_to_vec(d)
> v = Array(Float64, 0)
> for k in keys(d)
> push!(v, d[k])
> end
> return v
> end
>
> The problem is that if I convert a vector to a dictionary first, and then
> convert it back to a vector, the vector is not ordered anymore. As an
> example, the output of this code:
>
> v0 = [1.0, 2.0, 3.0]
> ll = ["a", "b", "c"]
> d = vec_to_dict(v0, ll)
> v1 = dict_to_vec(d)
> println(v0)
> println(d)
> println(v1)
>
> is:
>
> [1.0,2.0,3.0]
> {"c"=>3.0,"b"=>2.0,"a"=>1.0}
> [3.0,2.0,1.0]
>
> Is there some way to get the initial vector back with such a transformation
> using a dictionary?

https://github.com/JuliaLang/DataStructures.jl#ordereddicts-and-orderedsets

>
> Thanks,


Re: [julia-users] Converting vectors to dictionaries back and forth

2015-09-01 Thread amiksvi
Exactly what I needed, thanks a lot.


Re: [julia-users] Converting vectors to dictionaries back and forth

2015-09-01 Thread Cedric St-Jean
BTW, if I'm not mistaken, your dict_to_vec function can be written as just 
collect(values(d)). 

On Tuesday, September 1, 2015 at 9:47:47 AM UTC-4, ami...@gmail.com wrote:
>
> Exactly what I needed, thanks a lot.
>