python - 特定值的 numpy 数组打印索引

标签 python arrays numpy indexing

给定一个 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/

相关文章:

用于计算两组 2d 点之间成对距离的 Python 替代方法

python - 设计矩阵函数的 Matlab 到 Python 翻译

python - 有效获取 3D NumPy 数组的第 i 个 2D 切片的第 i 列,对于所有 i

python - 具有透明背景的 mplot3d 动画

python - 根据状态 :Available using Python? 过滤或查询 AWS RDS DBInstances 的正确方法是什么

javascript - 基于一个公共(public)键angular 2合并2个数组

c - 在另一个 .c 文件的函数中使用函数?

c - 逐行读取的 shell

python - 按日期时间间隔计算记录数

python - Pygame 只绘制屏幕的 1/4