如何使用{value:(row#,column#)}对字典替换2D numpy数组中的值 [英] How do I replace values in 2D numpy array using a dictionary of {value:(row#,column#)} pairs

查看:51
本文介绍了如何使用{value:(row#,column#)}对字典替换2D numpy数组中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 import numpy as np

数组看起来像这样:

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

字典是这样的:

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

我要遍历数组,并将与字典中的网格坐标匹配的所有栅格点替换为字典中的相应值

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

我在这样的输出之后:

 [ 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.]]

** 我已经编辑了问题,提供位于阵列内的坐标,除了 1 个例外.我还提供了所需输出的示例

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

推荐答案

在替换数组之前,我们需要先将值=> coordinates转换为coordinates => values的映射.我已经编辑了字典条目以进行演示,并且正如注释中所指出的,字典坐标条目应小于数组的尺寸

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

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 => 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)}

反转映射:

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

替换:

#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.]])

类型转换:

只要数组坐标和字典条目的类型相同,您就不会遇到任何错误/警告.如果您更喜欢int/float

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}

这篇关于如何使用{value:(row#,column#)}对字典替换2D numpy数组中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆