根据 Python 中的其中一个数组对 3 个单独的 numpy 数组进行排序

Sorting 3 seperate numpy arrays based on one of them in Python

我将根据一维数组值对以下二维和一维 numpy 数组进行排序,但出现了 ValueError:

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

数组是:

import numpy as np
a = np.array(
    [[0, 1, 0, 1, 1, 1, 1],
     [1, 0, 0, 1, 1, 0, 1],
     [0, 1, 1, 1, 1, 0, 0,],
     [0, 0, 0, 0, 1, 0, 0,],
     [0, 0, 1, 0, 1, 1, 0,]])

b = np.array(
    [[ -9.,   6.],
     [ -5.,  -6.],
     [ -9.,  -4.],
     [ -4.,   2.],
     [-13.,   3.]])

c = np.array([ 4.,  2.,  1.,  6.,  1.])

c, a, b = zip(*sorted(zip(c,a,b)))

澄清一下,如何根据c的值对a、b、c的值进行排序?

IIUC,这是 numpy.argsort:

的完美用例
idx = np.argsort(c)

c[idx]
# array([1., 1., 2., 4., 6.])

a[idx]
# array([[0, 1, 1, 1, 1, 0, 0],
#        [0, 0, 1, 0, 1, 1, 0],
#        [1, 0, 0, 1, 1, 0, 1],
#        [0, 1, 0, 1, 1, 1, 1],
#        [0, 0, 0, 0, 1, 0, 0]])

b[idx]
# array([[ -9.,  -4.],
#        [-13.,   3.],
#        [ -5.,  -6.],
#        [ -9.,   6.],
#        [ -4.,   2.]])