给定一个 numpy 数组
A = np.array([[[29, 64, 83],
[17, 92, 38],
[67, 34, 20]],
[[73, 28, 45],
[19, 84, 61],
[22, 63, 49]],
[[48, 30, 13],
[11, 52, 86],
[62, 25, 12]]])
我想要某个值的索引,比如说63
该值不可能重复或丢失
我做了
idx = np.where(A == 63)
print(idx)
我得到了
(array([1], dtype=int32), array([2], dtype=int32), array([1], dtype=int32))
我想要的是
[1, 2, 1]
作为列表或其他没有所有 array、dtype=int32
等的可迭代
我该怎么做?
最佳答案
如果你想取回一个 numpy 数组,只需使用 concatenate功能:
In [30]: np.concatenate(idx)
Out[30]: array([1, 2, 1])
如果您真的对 Python 列表感兴趣,那么只需:
In [31]: np.concatenate(idx).tolist()
Out[31]: [1, 2, 1]
关于python - 特定值的 numpy 数组打印索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25066978/