如何使用{value :( row#,column#)}}

时间:2016-09-12 11:35:47

标签: python numpy dictionary

 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个例外,提供坐在数组中的坐标。我还提供了所需输出的示例

2 个答案:

答案 0 :(得分:3)

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

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的解决方案。

答案 1 :(得分:1)

我们需要在数组中替换之前将值从值=>坐标转换为坐标=>值。我已经编辑了字典条目以用于演示目的,并且如评论中所指出的,字典坐标条目应该小于数组的维度

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}