TypeError: only integer scalar arrays can be converted to a scalar index

Posted latup

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError: only integer scalar arrays can be converted to a scalar index相关的知识,希望对你有一定的参考价值。

问题描述:在访问列表中的元素时出现报错:TypeError: only integer scalar arrays can be converted to a scalar index

1 voteIlabel = labels[sortedDistIndicies[i]]

假设a为列表,如果访问a[object]时出现上述错误,可以参考下面的内容

 

原因描述:出现这种情况一般是因为,a[object]中object应为整数,而你所提供给列表的object不是整数,所以不能作为列表的索引去访问列表元素

 

建议:使用语句print(type(object))查看一下你所提供的object是不是整数,如果不是,那么只需要根据需要提供整数

    如,上面代码中的sortedDistIndicies[i],我以为是得到的是整数,结果它是一个数组,所以编译器表示只有标量整数才可以作为标量索引,之后我将代码改成sortedDistIndicies[0][i]后得到一个整数,通过编译

以上是关于TypeError: only integer scalar arrays can be converted to a scalar index的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf

解决问题:only integer scalar arrays can be converted to a scalar index

在 Electron 中,使用“typeof”会导致 TypeError: Cannot assign to read only property

TypeError: can only concatenate str (not “int“) to str

成功解决TypeError: only size-1 arrays can be converted to Python scalars

请解决这个 TypeError: can only concatenate str (not "int") to str