python - 有没有办法在二维数组中找到输入值的每个索引?

标签 python arrays pandas numpy indexing

我一直在尝试在 python 中创建一个函数,该函数返回二维数组中每个重复出现的值的 x、y 坐标。例如,如果我有数组和一个值,

array = [ [1 ,2 ,3]
          [2 ,3 ,1]
          [3 ,2, 1]]

search = 1

它会输出(0,0) (1,2) (2,2)

我一直在尝试使用一些函数,例如 np.where 或将其转换为 pandas 数据框并以这种方式进行搜索,但我不确定最好的方法。当我使用 np.where 时,它​​返回一个空数组,因为我使用的是长小数。我正在尝试在 200 x 200 的数组上执行此操作。

最佳答案

我们可以做 np.where PS:a 是你的数组

list(zip(*np.where(a==search)))
[(0, 0), (1, 2), (2, 2)]

正如 hpaulj 提到的那样

np.argwhere(np.isclose(a,search))
array([[0, 0],
       [1, 2],
       [2, 2]])

关于python - 有没有办法在二维数组中找到输入值的每个索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62419574/

相关文章:

python - 使用 Geopandas 时,模块 'shapely' 没有属性 'geometry'

python - 根据名称进行字符串分组

c - 如何将 char *a[] 转换为 char **?

c++ - 下界上界给出相同的结果

python - 如果当前日期在列中则显示行

当脚本不在同一文件夹中时,Python、os.system 失败

python - 面向对象的 Python 程序计算球体的体积和表面积

arrays - 如何从数组中读取前n个元素

Python 数据框 : simple string split that includes '-'

python - 将一个元素添加到 pandas dataframe 中的数组末尾