有一个二维数组是这样的:
img = [
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[2, 2, 2], [3, 2, 3], [6, 7, 6]],
[[9, 8, 1], [9, 8, 3], [9, 8, 5]]
]
我只想获取某些像这样的索引的总和:
indices = [[0, 0], [0, 1]] # which means img[0][0] and img[0][1]
# means here is represents
在该链接中也有类似的关于stackoverflow中的1-d数组的问题,但是当我尝试使用它时出现了错误print(img[indices])
。因为我想弄清楚的元素img
是那些由索引指示的元素,然后获取 意思 总和。
预期产量
[5, 7, 9]
使用NumPy:
import numpy as np
img = np.array(img)
img[tuple(indices)].sum(axis = 0)
#array([5, 7, 9])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句