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.

>
> 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