1

I have a dictionary containing a variable number of numpy arrays (all same length), each array is stored in its respective key.

For each index I want to replace the value in one of the arrays by a newly calculated value. (This is a very simplyfied version what I'm actually doing.)

The problem is that when I try this as shown below, the value at the current index of every array in the dictionary is replaced, not just the one I specify.

Sorry if the formatting of the example code is confusing, it's my first question here (Don't quite get how to show the line example_dict["key1"][idx] = idx+10 properly indented in the next line of the for loop...).

>>> import numpy as np 

>>> example_dict = dict.fromkeys(["key1", "key2"], np.array(range(10)))

>>> example_dict["key1"]

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> example_dict["key2"]

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> for idx in range(10):
    example_dict["key1"][idx] = idx+10



>>> example_dict["key1"]

array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])

>>> example_dict["key2"]

array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])

I expected the loop to only access the array in example_dict["key1"], but somehow the same operation is applied to the array stored in example_dict["key2"] as well.

1 Answers1

1
>>> hex(id(example_dict["key1"]))
'0x26a543ea990'
>>> hex(id(example_dict["key2"]))
'0x26a543ea990'

example_dict["key1"] and example_dict["key2"] are pointing at the same address. To fix this, you can use a dict comprehension.

import numpy
keys = ["key1", "key2"]
example_dict = {key: numpy.array(range(10)) for key in keys}
yklcs
  • 306
  • 2
  • 6
  • Thank you, it fixes the problem. Although I am wondering why the function made for exactly this specific purpose (`dict.fromkeys()`) would introduce this problem in the first place... – JonasKaminski Jun 11 '19 at 14:35