The following code:
x = list(range(0,10))
random.shuffle(x)
ind = np.argsort(x)
x[ind]
produces the error: TypeError: only integer scalar a
Good answer - but I would add that if the list cannot be converted to a numpy array (i.e. you have a list of string) you cannot slice it with an array of indices as described above. The most straightforward alternative is
[x[i] for i in ind]
The problem is that I am attempting to index x
, an ordinary Python list, as if it were a numpy array. To fix it, simply convert x
to a numpy array:
x = list(range(0,10))
random.shuffle(x)
ind = np.argsort(x)
x = np.array(x) # This is the key line
x[ind]
(This has happened to me twice now.)