相关疑难解决方法(0)

查找已排序数组中元素的位置

假设我有一些 numpy 数组(所有元素都是唯一的),我想按降序排序。我需要找出初始数组的元素将在排序数组中占据哪些位置。

例子。

In1: [1, 2, 3] # Input

Out1: [2, 1, 0] # Expected output

In2: [1, -2, 2] # Input

Out2: [1, 2, 0] # Expected output
Run Code Online (Sandbox Code Playgroud)

我尝试过这个:

def find_positions(A):
    A = np.array(A)
    A_sorted = np.sort(A)[::-1]
    return np.argwhere(A[:, None] == A_sorted[None, :])[:, 1]
Run Code Online (Sandbox Code Playgroud)

但当输入数组非常大(len > 100000)时它不起作用。我做错了什么以及如何解决?

python numpy

2
推荐指数
1
解决办法
1929
查看次数

标签 统计

numpy ×1

python ×1