【问题标题】:How do I replace values in 2D numpy array using a dictionary of {value:(row#,column#)} pairs如何使用 {value:(row#,column#)} 对的字典替换 2D numpy 数组中的值
【发布时间】:2026-01-05 20:45:02
【问题描述】:
 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)}

我想遍历数组,用字典中对应的值替换任何匹配字典中网格坐标的网格点

我想要这样的输出:

 [ 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 个例外。我还提供了所需输出的示例

【问题讨论】:

  • 你的预期输出是什么?
  • 您的意思可能是“..in the dictionary values with相应的keys”对吧?值是元组(坐标),键是分号之前的内容(:)。请注意,在您的示例中,字典中的所有坐标都不在数组中。
  • 1) 是的,当我说价值时,我的意思是说键。抱歉,2)另外,当我把那个 10x10 数组放在问题中时,我忘了缩小我的坐标。我当然觉得发布 50x50 会太大而无法发布
  • 你如何得到字典?因为如果值可以是非唯一的,它就不能被视为很容易的字典
  • 所以应该忽略 (24,35) 条目?

标签: python numpy dictionary


【解决方案1】:

希望我能正确理解您的问题

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

for i in data.keys():
    try:
        array[data[i][0],data[i][1]] = float(i)
    except IndexError:
       pass
print array

我更改了索引,使其适合您的 10 x 10 数组(我假设您在实际示例中使用更大的数组)

我遍历字典中的所有键(值)。然后程序尝试在给定坐标的数组中设置这个值。 如果某些坐标在数组之外(如本例中的最后一个),我会传递 IndexErrors。

编辑

此解决方案仅适用于您的密钥是唯一的。如果不是,我会推荐@Osssan 的解决方案。

【讨论】:

  • 我忽略了唯一性,感谢您注意到这一点。坐标始终是唯一的,值可能不是。
  • 不幸的是,我的钥匙并非都是独一无二的。这是一个直方图输出,因此存在具有相同键的坐标。几个坐标也有零键
  • 请注意,字典的结构是{key:value},也可以颠倒这种关系并有新的字典映射{value:key}。 LHS 项称为键,RHS 项称为值。在您的情况下,几个坐标将具有相同的值,即值不是唯一的,但坐标将是唯一的。在我的解决方案中,我已将您的字典转换为基于坐标的字典,因此应该适用于您的情况。如果有任何问题,请您尝试并报告
  • data[i][0],data[i][1] 拼写更好data[i]
【解决方案2】:

在替换数组之前,我们需要反转从 values=>coordinates 到 co-ordinates=>values 的映射。我已经编辑了用于演示目的的字典条目,正如 cmets 中所指出的,字典坐标条目应该小于数组的维度

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,您还可以强制执行特定的类型转换

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

【讨论】:

  • 在 cmets 中,Eric 提到了这个输入 {72: (3, 4), 11: (3, 4)}。这个输入是否可能,如果可以,你期望什么输出
  • 这是不可能的。所以我应该把它排除在外。我真实数据集中的所有坐标都在数组的范围内。我实际上正在使用 100x100 数组,坐标是真实的纬度和经度点,已被插值到 0 - 100
  • 如果您没有进一步的疑问,您可以通过接受最能满足您需求的解决方案来结束问题。
  • 我给出的示例字典的坐标是整数。您用来提供解决方案的那个具有浮点数坐标。在使用 numpy 数组时,这种差异是否重要?
  • 我不知道 numpy 数组的运行时类型转换和内部工作原理,但就个人而言,我会确保数组条目和字典项之间的类型匹配以避免潜在的错误/警告。已编辑帖子以将 cast float 类型转换为 int,将 int() 调用替换为 float 对于计数器场景来说很简单。
最近更新 更多