确定两个numpy数组在Python中相交的参数

时间:2013-08-28 10:58:08

标签: python arrays numpy indexing set

我有两个数组,比如说:

a, b = np.array([13., 14., 15., 32., 33.]), np.array([15., 16., 17., 33., 34., 47.])

我需要找到b中不存在的所有元素的索引。 在上面的例子中,结果将是:

[0, 1, 3]

因为[0],[1]和[3]是13.,14和32.,它们不存在于b中。请注意,我不知道13,14和32的实际值。(在这种情况下,我可以使用set(a).difference(set(b)))。我真的只对指数感兴趣。

如果可能,答案应该是“矢量化”,即不使用for循环。

3 个答案:

答案 0 :(得分:3)

您可以使用np.in1d

>>> np.arange(a.shape[0])[~np.in1d(a,b)].tolist()
  [0, 1, 3]

答案 1 :(得分:2)

这很简单,使用numpy.intersect1d计算ab之间共享的元素,然后使用{{3}检查哪些元素不在a中最后使用numpy.in1d在数组中获取它们的位置。

>>> import numpy as np
>>> a, b = np.array([13., 14., 15., 32., 33.]), np.array([15., 16., 17., 33., 34., 47.])
>>> np.argwhere(np.in1d(a, np.intersect1d(a,b)) == False)
array([[0],
   [1],
   [3]])

如果您更喜欢列表,只需添加.flatten即可将矩阵转换为向量,然后应用.tolist获取列表:

>>> np.argwhere(np.in1d(a, np.intersect1d(a,b)) == False).flatten().tolist()
 [0, 1, 3]

答案 2 :(得分:1)

如果使用循环,则相当直接:

def difference_indices(a, b):

    # Set to put the unique indices in
    indices = []

    # So we know the index of the element of a that we're looking at
    a_index = 0

    for elem_a in a:

        found_in_b = False
        b_index = 0

        # Loop until we find a match. If we reach the end of b without a match, the current 
        # a index should go in the indices list
        while not found_in_b and b_index < len(b):
            if elem_a == b[b_index]: found_in_b = True
            b_index = b_index + 1

        if not found_in_b: indices.append(a_index)
        a_index = a_index + 1

    return indices

这应该适用于包含任何一种类型的列表,只要它们是相同的类型,并且为该类型定义了__eq__函数。

没有循环这样做需要知道python比我的更大!希望这对你有用。