Python/TypeError: only integer scalar arrays can be converted to a scalar index
根据实际解决,只有是numpy
类型的数组,才能用列表/数组类型作为索引。
即numpy_array[list/numpy_array]
如果正在使用的不是numpy
构建的,解决的方法是用np.array(your_array)
。
注:事先import numpy as np
举例
>>> a = [1,2,3]
>>> b = [1,2,3,4,5,6]
>>> na = np.array([1,2,3])
>>> nb = np.array([1,2,3,4,5,6])
>>> b[a]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not list
>>> b[na]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: only integer scalar arrays can be converted to a scalar index
>>> nb[a]
array([2, 3, 4])
>>> nb[na]
array([2, 3, 4])
更多
Python
数据科学相关见专题Python数据科学技能索引