【问题标题】:Determine arguments where two numpy arrays intersect in Python确定 Python 中两个 numpy 数组相交的参数
【发布时间】:2013-08-31 09:56:53
【问题描述】:

我有两个数组,比如说:

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

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

[0, 1, 3]

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

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

【问题讨论】:

  • 在这个例子中只是巧合,它们都是排序数组吗? (如果它们在您的问题的真实版本中排序,您可以滥用该属性)
  • 对不起,我使用排序数组来帮助阅读。但我仍然很想听听您将如何处理排序数组 :)
  • 好吧,自定义算法可能会通过滥用它们已排序的事实来获得更好的复杂性,(不确定最终会得到什么复杂性,但我假设比你做的更好,如果你没有那个属性)

标签: python arrays numpy indexing set


【解决方案1】:

你可以使用np.in1d:

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

【讨论】:

    【解决方案2】:

    这很简单,使用numpy.intersect1d 计算ab 之间共享的元素,然后使用numpy.in1d 检查哪些元素不在a 中,最后使用numpy.argwhere.

    >>> 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]
    

    【讨论】:

    • 我不知道这三种方法中的任何一种。太好了!
    【解决方案3】:

    如果您使用循环,则相当简单:

    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 知识!希望这对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 2021-05-17
      相关资源
      最近更新 更多