Python 当存在重复索引时,numpy是否会保持赋值顺序?

Python 当存在重复索引时,numpy是否会保持赋值顺序?,python,arrays,numpy,Python,Arrays,Numpy,我想按索引数组对数组进行赋值,但存在重复的索引 例如: a = np.arange(5) index = np.array([1,2,3,1,2,3,1,2,3]) b = np.arange(9) a[index] = b 两个问题: 对于重复索引,最新分配是否始终生效 a[1]==6是否适用于任何情况,例如对于非常大的数组a?是否可能a[1]==0或3 更具体地说,我使用了用MKL编译的numpy(由Anaconda提供),一些数组操作是并行的 相关职位: 如果上面的答案是否,是否有任何方

我想按索引数组对数组进行赋值,但存在重复的索引

例如:

a = np.arange(5)
index = np.array([1,2,3,1,2,3,1,2,3])
b = np.arange(9)
a[index] = b
两个问题:

  • 对于重复索引,最新分配是否始终生效

    a[1]==6是否适用于任何情况,例如对于非常大的数组
    a
    ?是否可能
    a[1]==0
    3

    更具体地说,我使用了用MKL编译的numpy(由Anaconda提供),一些数组操作是并行的

    相关职位:

  • 如果上面的答案是,是否有任何方法可以确保作业始终保持有序


  • 这里有一种方法可以保证从相同的索引组中分配到最后一个索引-

    # Get sorting indices for index keeping the order with 'mergesort' option
    sidx = index.argsort(kind='mergesort')
    
    # Get sorted index array
    sindex = index[sidx]
    
    # Get the last indices from each group of identical indices in sorted version
    idx = sidx[np.r_[np.flatnonzero(sindex[1:] != sindex[:-1]), index.size-1]]
    
    # Use those last group indices to select indices off index and b to assign
    a[index[idx]] = b[idx]
    
    样本运行-

    In [141]: a
    Out[141]: array([0, 1, 2, 3, 4])
    
    In [142]: index
    Out[142]: array([1, 2, 3, 1, 2, 1, 2, 3, 4, 2])
    
    In [143]: b
    Out[143]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    In [144]: sidx = index.argsort(kind='mergesort')
         ...: sindex = index[sidx]
         ...: idx = sidx[np.r_[np.flatnonzero(sindex[1:] != sindex[:-1]), index.size-1]]
         ...: a[index[idx]] = b[idx]
         ...: 
    
    In [145]: a
    Out[145]: array([0, 5, 9, 7, 8])
    

    一个更简单的等价于Divakar的解

    def assign_last(a, index, b):
        """a[index] = b
        """
        index = index[::-1]
        b = b[::-1]
    
        ix_unique, ix_first = np.unique(index, return_index=True)
        # np.unique: return index of first occurrence.
        # ix_unique = index[ix_first]
    
        a[ix_unique] = b[ix_first]
        return a
    
    a =  array([0, 1, 2, 3, 4])
    index = array([1, 2, 3, 1, 2, 1, 2, 3, 4, 2])
    b = array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    assign_last(a, index, b)
    
    输出

    array([0, 5, 9, 7, 8])
    

    可能重复的FYI,->“对于高级分配,通常不保证迭代顺序。这意味着如果元素设置多次,就不可能预测最终结果。”这是一个很好的解决方案。很高兴知道
    mergeosort
    可以维持秩序。非常感谢。