On Tue, Sep 1, 2015 at 9:39 AM,  <amik...@gmail.com> 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,

Reply via email to