简体   繁体   English

使用np.where查找2D数组中的元素索引会产生ValueError

[英]Using np.where to find index of element in 2D array gives ValueError

I'm trying to use np.where to find the index of an element in an array, specifically row number 我正在尝试使用np.where来查找数组中元素的索引,特别是行号

I have an array of say size 1000 x 6, named 'table'. 我有一个大小为1000 x 6的数组,名为'table'。 The first element in each row is a 2 x 2 string array, and the rest are 0s. 每行中的第一个元素是一个2 x 2字符串数组,其余的是0。 Eg. 例如。 a 5 x 6 example of elements in 'table': 'table'中5 x 6元素的示例:

    [['s',' ']   0 0 0 0 0
     [' ',' ']]
    [[' ',' ']   0 0 0 0 0
     [' ','a']]
    [[' ',' ']   0 0 0 0 0
     [' ',' ']]         
    [['p',' ']   0 0 0 0 0
     [' ',' ']]
    [[' ',' ']   0 0 0 0 0
     ['b',' ']]  

The 2x2 arrays are all different, and I want to get the index, in particular the row number, of the one containing a specific 2x2 in my large table. 2x2数组都是不同的,我想得到我的大表中包含特定2x2的索引,特别是行号。

Eg. 例如。 say I have 说我有

    grid = [['s',' ']   
            [' ',' ']]

I would like my code to return [0][0] 我希望我的代码返回[0] [0]

I have tried this: 我试过这个:

    i,j = np.where(table == grid)

and also 并且

    i,j = np.where(np.all(table == grid))

and i get the following error: 我收到以下错误:

    ValueError: not enough values to unpack (expected 2, got 1)

Using a single value eg. 使用单个值,例如。

    index = np.where(table == grid) 

does not result in an error, but print(index) will output an empty array: 不会导致错误,但print(index)将输出一个空数组:

    (array([], dtype=int64),)

From similar questions on Stack Overflow I can't seem to figure out how this error applies to mine and I've been staring at it for ages 从Stack Overflow上的类似问题我似乎无法弄清楚这个错误是如何适用于我的并且我已经盯着它多年了

Any help would be much appreciated 任何帮助将非常感激

Setup: 设定:

b = np.array([['s','t'],['q','r']])
c = np.array([['s',' '],[' ',' ']])
a = np.array([[c,0,0,0,0,0],
              [c,0,0,0,0,0],
              [c,0,0,0,0,0],
              [c,0,0,0,0,0],
              [b,0,0,0,0,0],
              [c,0,0,0,0,0],
              [c,0,0,0,0,0],
              [c,0,0,0,0,0],
              [c,0,0,0,0,0]])

Assuming your only interested in column zero; 假设你只对零感兴趣; write a function that will test each item in a one-d array. 编写一个函数来测试一维数组中的每个项目。 And apply it to column zero 并将其应用于第0列

def f(args):
    return [np.all(thing==b) for thing in args]

>>> np.apply_along_axis(f,0,a[:,0])
array([False, False, False, False,  True, False, False, False, False])
>>> 

Use np.where on the result 在结果上使用np.where

>>> np.where(np.apply_along_axis(f,0,a[:,0]))
(array([4], dtype=int64),)

Or following the note in the numpy.where docs: 或者遵循numpy.where文档中的numpy.where

>>> np.asarray(np.apply_along_axis(f,0,a[:,0])).nonzero()
(array([4], dtype=int64),)

As @hpaulj points out np.apply_along_axis is not necessary. 正如@hpaulj所指出的那样,没有必要使用np.apply_along_axis so 所以

>>> [np.all(thing == b) for thing in a[:,0]]
[False, False, False, False, True, False, False, False, False]

>>> np.asarray([np.all(thing == b) for thing in a[:,0]]).nonzero()
(array([4], dtype=int64),)

And without the Python iteration: 没有Python迭代:

>>> (np.stack(a[:,0])==b).all(axis=(1,2))
array([False, False, False, False,  True, False, False, False, False])

>>> (np.stack(a[:,0])==b).all(axis=(1,2)).nonzero()
(array([4], dtype=int64),)

Here is a solution using vectorize 这是使用矢量化的解决方案

a = np.array( [np.array([['s',' '],[' ',' ']])  , 0, 0, 0, 0, 0 ])
grid = np.array([['s',' '],[' ',' ']]) 

vfunc = np.vectorize(lambda x: np.all(grid == x))
np.argwhere(vfunc(a))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM