I updated the README with another example based on this discussion.

Cheers!
   Kevin

On Fri, Jul 3, 2015 at 7:51 AM, Yichao Yu <[email protected]> wrote:

> On Fri, Jul 3, 2015 at 10:40 AM, Yichao Yu <[email protected]> wrote:
> > On Fri, Jul 3, 2015 at 9:42 AM, Ali Rezaee <[email protected]> wrote:
> >> Thank you. What would be the work around? How can I push values to
> >> defaultdict without changing any other value?
> >
> > The README I linked is exactly about that.
>
> And quote it here for future reference:
>
> > Note that in the last example, we need to use a function to create each
> new `DefaultDict`. If we forget, we will end up using the same
> `DefaultDict` for all default values:
>
> In their case, the value of a parent DefaultDict is also a DefaultDict
> so it might be a little confusing. You can pretty much replace the
> DefaultDict above by `Array` and it will apply to your case directly.
>
>
> >
> >>
> >> On Friday, July 3, 2015 at 3:35:39 PM UTC+2, Yichao Yu wrote:
> >>>
> >>> On Fri, Jul 3, 2015 at 9:29 AM, Ali Rezaee <[email protected]> wrote:
> >>> > Hi,
> >>> >
> >>> > I have the following code:
> >>> >
> >>> > using DataStrucures
> >>> > d= DefaultDict(ASCIIString,Array{Int64},Array{Int64}(0))
> >>> >
> >>> > push!(d["A"],2)
> >>> > push!(d["B"],3)
> >>> >
> >>> > d
> >>> > DataStructures.DefaultDict{ASCIIString,Array{Int64,N},Array{Int64,1}}
> >>> > with 2
> >>> > entries:
> >>> >   "B" => [2,3]
> >>> >   "A" => [2,3]
> >>> >
> >>> >
> >>> > Isn't it unexpected that when I push to any key, the value is pushed
> to
> >>> > all
> >>> > the other keys as well?
> >>>
> >>> Yes, pretty much. Because the default value is stored by reference and
> >>> you are mutating it with `push!`
> >>>
> >>> See the last paragraph of the corresponding section in the
> >>> DataStructures.jl README[1]
> >>>
> >>> [1]
> >>>
> https://github.com/JuliaLang/DataStructures.jl#defaultdict-and-defaultordereddict
> >>>
> >>> >
> >>> > Thanks :)
>

Reply via email to