比较python中的multidimesional数组的行

时间:2015-09-14 07:45:20

标签: python arrays list numpy scipy

我有两个numpy数组

a= np.array([[2,2],[3,2],[4,2],[3,3],[5,3]])
b= np.array([[1,1],[1,3],[5,3]])

我想将a与b进行比较并返回a-b,以便:

a-b = array([[2,2],
             [3,2],
             [4,2],
             [3,3]]) 

我尝试过:

[x for x in a if x not in b] 

导致

[array([2, 2]), array([3, 2]), array([4, 2])] # where clearly [3,3] is missing

我甚至尝试比较循环中a和b的每一行,它给出了一个错误

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

任何人都可以帮我解决这个问题???

2 个答案:

答案 0 :(得分:2)

基于

Broadcasting的矢量化方法 -

a[~((b[:,None,:] == a).all(2)).any(0)]

使用cdist中的scipy.spatial.distance -

from scipy.spatial.distance import cdist

a[~(cdist(a,b)==0).any(1)]

示例运行 -

In [89]: a
Out[89]: 
array([[2, 2],
       [3, 2],
       [4, 2],
       [3, 3],
       [5, 3]])

In [90]: b
Out[90]: 
array([[1, 1],
       [1, 3],
       [5, 3]])

In [91]: a[~((b[:,None,:] == a).all(2)).any(0)]
Out[91]: 
array([[2, 2],
       [3, 2],
       [4, 2],
       [3, 3]])

In [92]: a[~(cdist(a,b)==0).any(1)]
Out[92]: 
array([[2, 2],
       [3, 2],
       [4, 2],
       [3, 3]])

答案 1 :(得分:1)

执行此操作的一种方法是将numpy数组转换为元组列表,将b转换为元组集,然后执行您在其上使用的相同列表解析。示例 -

In [1]: import numpy as np

In [2]: a= np.array([[2,2],[3,2],[4,2],[3,3],[5,3]])

In [3]: b= np.array([[1,1],[1,3],[5,3]])

In [18]: alist = list(map(tuple, a))

In [19]: bset = set(map(tuple, b))

In [20]: np.array([x for x in alist if x not in bset])
Out[20]:
array([[2, 2],
       [3, 2],
       [4, 2],
       [3, 3]])