Skip to content Skip to sidebar Skip to footer

How Do I Replace Values In 2d Numpy Array Using A Dictionary Of {value:(row#,column#)} Pairs

import numpy as np the array looks like so: 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.

Solution 1:

I hope I understood your question correctly

array=np.zeros((10,10))data= {72:(3, 4), 11:(1, 5), 10:(2, 4), 43:(2, 3), 22:(24,35)}

foriindata.keys():try:array[data[i][0],data[i][1]]=float(i)except IndexError:passprintarray

I changed the indices such that it fits into your 10 x 10 array (I assume you work with a bigger array in your real example)

I iterate over all keys in the dictionary (the values). The program then tries to set this value in the array at the given coordinates. I pass IndexErrors for the case some coordinates are outside the array (like the last in this example.

EDIT

This solution only works if your keys are unique. If they are not I would recommend the solution of @Osssan.

Solution 2:

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

importnumpyasnparrObj=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 replacementreplaceArrObj=arrObj#ensure co-ordinates in the dictionary could be indexed in array#current mapping: values => co-ordinatesdictObj= {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)}

Invert Mapping:

#invert mapping of dictionary: co-ordinates => 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}

Replacement:

#Replace values from dictionary at correponding co-ordiantesfor 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.]])

Type Conversion:

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

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

Post a Comment for "How Do I Replace Values In 2d Numpy Array Using A Dictionary Of {value:(row#,column#)} Pairs"