Python:查找索引

标签 python arrays numpy find vectorization

如果我有 a 和 b:

a=[[1,2,3],
   [4,5,6],
   [7,8,9]]

b=8.1

我想在a中找到值b的索引,我可以这样做:

nonzero(abs(a-b)<0.5)

得到(2,1)作为索引,但是如果b是一维或者二维数组怎么办?说,

b=[8.1,3.1,9.1]

我想得到 (2,1),(0,2),(2,2)

一般来说,对于 b 的每个值,我希望 a 中只有一个匹配项。我可以避免 for 循环吗?

最佳答案

使用列表理解:

[nonzero(abs(x-a)<0.5) for x in b]

关于Python:查找索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34687530/

相关文章:

python - 在一列中存储多个值

c - 将 char 数组传递给采用 char 指针的函数

python - 整数和数组排列之间的惰性映射,无需生成所有排列

python - 稀疏矩阵点积每个结果行仅保留 N-max 值

python - ufunc bitwise_xor 的类型错误

Python-Tkinter :how to highlight item on taskbar on windows xp when window is minimized to the taskbar

Python如何获得用zlib压缩的数据长度?

c++ - 内存冲突 : SIGSEGV and 'can' t find linker symbol for virtual table. ..'

c - 二维数组中的 Free()

python - 如何使用 QStyledItemDelegate 只绘制背景,而不覆盖文本?