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]后得到一个整数,通过编译

posted @ 2018-10-12 19:13  Latuper  阅读(2840)  评论(0编辑  收藏  举报