首页 文章

尝试在numpy数组中使用整数列表时输入错误:只能将整数标量数组转换为标量索引

提问于
浏览
2

以下代码:

x = list(range(0,10))
random.shuffle(x)
ind = np.argsort(x)
x[ind]

产生错误:TypeError:只能将整数标量数组转换为标量索引

请注意,我的问题与问题“numpy array TypeError: only integer scalar arrays can be converted to a scalar index”不同,后者正在尝试更复杂的问题 .

我在这个问题上的错误是我试图在普通的python列表中使用索引列表 - 请参阅我的回答 . 我预计它比使用范围和随机播放要广泛得多 .

1 回答

  • 2

    问题是我试图索引 x ,一个普通的Python列表,好像它是一个numpy数组 . 要修复它,只需将 x 转换为numpy数组:

    x = list(range(0,10))
    random.shuffle(x)
    ind = np.argsort(x)
    x = np.array(x) # This is the key line
    x[ind]
    

    (这已经发生在我身上两次了 . )

相关问题