How do I replace values in 2D numpy array using a dictionary of {value:(row#,column#)} pairs
<pre><code> import numpy as np
</code></pre>

<p>the array looks like so:</p>

<pre><code> array = np.zeros((10,10))
 array = 
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
</code></pre>

<p>the dictionary is like this:</p>

<pre><code> dict = {72: (3, 4), 11: (1, 5), 10: (2, 4), 43: (2, 3), 22: (24,35), 11: (8, 9)}
</code></pre>

<p>I want to iterate over the array and replace any grid points that match the grid coordinates in the dictionary with the corresponding value in the dictionary</p>

<p>i am after an output like this:</p>

<pre><code> [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0. 11.  0.  0.  0.  0.]
 [ 0.  0.  0. 43. 10.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0. 72.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0. 11.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
</code></pre>

<p>** i have edited the question the provide co-ordinates that sit within the array except for 1 exception. I also provided an example of the desired output</p>

<p>We need to invert mapping from values=>coordinates to co-ordinates=>values before replacement in the array. I have edited the dictionary entries for demo purpose and as pointed in comments, dictionary co-ordinate entries should be less than dimensions of array</p>

<pre><code>import numpy as np



arrObj = np.zeros((10,10))
arrObj

# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]

#copy of array for replacement
replaceArrObj=arrObj 


#ensure co-ordinates in the dictionary could be indexed in array
#current mapping: values =&gt; co-ordinates
dictObj = {1.0:(0.0,0.0),2.0:(1.0,1.0),3.0: (2.0, 2.0), 4.0: (3.0, 3.0),5.0:(4.0,4.0), 6.0: (5.0, 5.0), 7.0: (6.0, 6.0), 8.0: (7.0,7.0), 9.0: (8.0,8.0),
10.0: (9.0,9.0)}
dictObj
#{1.0: (0.0, 0.0),
# 2.0: (1.0, 1.0),
# 3.0: (2.0, 2.0),
# 4.0: (3.0, 3.0),
# 5.0: (4.0, 4.0),
# 6.0: (5.0, 5.0),
# 7.0: (6.0, 6.0),
# 8.0: (7.0, 7.0),
# 9.0: (8.0, 8.0),
# 10.0: (9.0, 9.0)}
</code></pre>

<p><strong>Invert Mapping:</strong></p>

<pre><code>#invert mapping of dictionary: co-ordinates =&gt; values
inv_dictObj = {v: k for k, v in dictObj.items()}
inv_dictObj
#{(0.0, 0.0): 1.0,
# (1.0, 1.0): 2.0,
# (2.0, 2.0): 3.0,
# (3.0, 3.0): 4.0,
# (4.0, 4.0): 5.0,
# (5.0, 5.0): 6.0,
# (6.0, 6.0): 7.0,
# (7.0, 7.0): 8.0,
# (8.0, 8.0): 9.0,
# (9.0, 9.0): 10.0}
</code></pre>

<p><strong>Replacement:</strong></p>

<pre><code>#Replace values from dictionary at correponding co-ordiantes
for i,j in inv_dictObj.keys():
    replaceArrObj[i,j]=inv_dictObj[(i,j)]



replaceArrObj
#array([[  1.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
#       [  0.,   2.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
#       [  0.,   0.,   3.,   0.,   0.,   0.,   0.,   0.,   0.,   0.],
#       [  0.,   0.,   0.,   4.,   0.,   0.,   0.,   0.,   0.,   0.],
#       [  0.,   0.,   0.,   0.,   5.,   0.,   0.,   0.,   0.,   0.],
#       [  0.,   0.,   0.,   0.,   0.,   6.,   0.,   0.,   0.,   0.],
#       [  0.,   0.,   0.,   0.,   0.,   0.,   7.,   0.,   0.,   0.],
#       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   8.,   0.,   0.],
#       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   9.,   0.],
#       [  0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,  10.]])
</code></pre>

<p><strong>Type Conversion:</strong></p>

<p>You should not face any errors/warnings as long as both array co-ordinates and dictionary entries have same type.
    You can additionally enforce specific type conversion if you prefer int/float</p>

<pre><code>#float to int conversion in array

replaceArrObj.astype(int)
#array([[ 1,  0,  0,  0,  0,  0,  0,  0,  0,  0],
#       [ 0,  2,  0,  0,  0,  0,  0,  0,  0,  0],
#       [ 0,  0,  3,  0,  0,  0,  0,  0,  0,  0],
#       [ 0,  0,  0,  4,  0,  0,  0,  0,  0,  0],
#       [ 0,  0,  0,  0,  5,  0,  0,  0,  0,  0],
#       [ 0,  0,  0,  0,  0,  6,  0,  0,  0,  0],
#       [ 0,  0,  0,  0,  0,  0,  7,  0,  0,  0],
#       [ 0,  0,  0,  0,  0,  0,  0,  8,  0,  0],
#       [ 0,  0,  0,  0,  0,  0,  0,  0,  9,  0],
#       [ 0,  0,  0,  0,  0,  0,  0,  0,  0, 10]])


#float to int conversion in dictionary, where k referes to key items and v to value items

int_dictObj = { (int(k[0]),int(k[1])):int(v) for k,v in inv_dictObj.items()}
int_dictObj
#{(0, 0): 1,
# (1, 1): 2,
# (2, 2): 3,
# (3, 3): 4,
# (4, 4): 5,
# (5, 5): 6,
# (6, 6): 7,
# (7, 7): 8,
# (8, 8): 9,
# (9, 9): 10}
</code></pre>

python numpy dictionary