我知道Python列表有一种方法可以返回第一个索引:
>>> l = [1, 2, 3]
>>> l.index(2)
1
Run Code Online (Sandbox Code Playgroud)
NumPy阵列有类似的东西吗?
在Python中,我们可以使用.index()获取数组中值的索引.我怎么能用NumPy数组做到这一点?
当我尝试做的时候
decoding.index(i)
Run Code Online (Sandbox Code Playgroud)
它说NumPy库不支持这个功能.有办法吗?
我需要弄清楚如何在2d numpy数组中找到值的所有索引.
例如,我有以下2d数组:
([[1 1 0 0],
[0 0 1 1],
[0 0 0 0]])
Run Code Online (Sandbox Code Playgroud)
我需要找到所有1和0的索引.
1: [(0, 0), (0, 1), (1, 2), (1, 3)]
0: [(0, 2), (0, 3), (1, 0), (1, 1), (the entire all row)]
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它没有给我所有的索引:
t = [(index, row.index(1)) for index, row in enumerate(x) if 1 in row]
Run Code Online (Sandbox Code Playgroud)
基本上,它只给我每行中的一个索引[(0, 0), (1, 2)].