比较Numpy数组会引发a.any()或a.all()错误

时间:2021-08-30 12:17:44

I'm trying the following code to check if elements of numpy array are found in another 'b' list but i get the following error

我正在尝试以下代码来检查numpy数组的元素是否在另一个'b'列表中找到但是我得到以下错误

The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()

i tried looking it up but couldn't get it to work

我试着查找但无法让它工作

how to use a.all() or other methods like numpy.logical_and in this example

如何在这个例子中使用a.all()或其他方法,如numpy.logical_and

import numpy as np
a=np.array([[0,0,0,0,1,0,1,1,0],[0,0,0,0,0,1,1,1,0],[0,0,0,0,0,0,1,1,1]])
b=[]

for item in a :
    if item not in b:
       b.append(item)`

2 个解决方案

#1


2  

Do it without loops:

没有循环这样做:

b = np.unique(a, axis=0)

This is orders of magnitude faster, and more clear.

这快了几个数量级,而且更加清晰。

#2


1  

Solution provided by @John Zwinck is probably the best for this problem. I'm commenting on the loop approach if for some reason one has to use it.

@John Zwinck提供的解决方案可能是解决此问题的最佳方案。如果由于某种原因必须使用它,我正在评论循环方法。

The problem with loop approach is the way the in and the array test for equality work.

循环方法的问题是in和array测试平等的方式。

Grosso modo, item in b compares item with the elements in b using equality (==). Something like:

Grosso modo,b中的项目使用相等性(==)将项目与b中的元素进行比较。就像是:

def in(x, y):
    for item in y:
        # Notice the use of == to compare two arrays, the result is an array of bool.
        if x == item:
            return True
    return False

So if one can't use np.unique and definitively needs to use a loop, then implement an variation of in to compare two arrays.

因此,如果一个人不能使用np.unique并且明确需要使用循环,那么实现in的变体来比较两个数组。

import numpy as np
a=np.array([[0,0,0,0,1,0,1,1,0],
            [0,0,0,0,0,1,1,1,0],
            [0,0,0,0,0,0,1,1,1]])
b=[]

def in_for_arrays(x, y):
    for item in y:
        # Two arrays are equal if ALL the elements are equal.
        if (x == item).all():
            return True
    return False


for item in a:
    if not in_for_arrays(item, b):
        b.append(item)

#1


2  

Do it without loops:

没有循环这样做:

b = np.unique(a, axis=0)

This is orders of magnitude faster, and more clear.

这快了几个数量级,而且更加清晰。

#2


1  

Solution provided by @John Zwinck is probably the best for this problem. I'm commenting on the loop approach if for some reason one has to use it.

@John Zwinck提供的解决方案可能是解决此问题的最佳方案。如果由于某种原因必须使用它,我正在评论循环方法。

The problem with loop approach is the way the in and the array test for equality work.

循环方法的问题是in和array测试平等的方式。

Grosso modo, item in b compares item with the elements in b using equality (==). Something like:

Grosso modo,b中的项目使用相等性(==)将项目与b中的元素进行比较。就像是:

def in(x, y):
    for item in y:
        # Notice the use of == to compare two arrays, the result is an array of bool.
        if x == item:
            return True
    return False

So if one can't use np.unique and definitively needs to use a loop, then implement an variation of in to compare two arrays.

因此,如果一个人不能使用np.unique并且明确需要使用循环,那么实现in的变体来比较两个数组。

import numpy as np
a=np.array([[0,0,0,0,1,0,1,1,0],
            [0,0,0,0,0,1,1,1,0],
            [0,0,0,0,0,0,1,1,1]])
b=[]

def in_for_arrays(x, y):
    for item in y:
        # Two arrays are equal if ALL the elements are equal.
        if (x == item).all():
            return True
    return False


for item in a:
    if not in_for_arrays(item, b):
        b.append(item)