且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用NumPy数组索引列表时发生TypeError:只能将整数标量数组转换为标量索引

更新时间:2021-12-18 04:01:56

问题是我试图索引x(一个普通的Python列表),就好像它是一个numpy数组一样.要解决此问题,只需将x转换为一个numpy数组:

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.)