TypeError: list indices must be integers or slices, not list

两盒软妹~` 提交于 2019-11-29 09:57:47

This is a classic mistake. i in your case is already an element from array (i.e. another list), not an index of array (not an int), so

if Volume == i[2]:
    counter += 1

Please, make sure to go at least through the beginning of Python tutorial, because this is very simple and fundamental stuff.

Also I would advise to stick to naming conventions: variables are normally lower-case (volume, not Volume). In this case i is misleading. row or elem would be much more suitable.

Also, as this may happen frequently, note that you cannot access slices of lists (but you can for an array):

import numpy as np
integerarray = np.array([33,11,22], dtype=int)
integerlist = [33,11,22]
indexArray = [1,2,0]  # or equivalently, an array, e.g. np.argsort(integerlist)
print(integerarray[indexArray]) ## works fine
print(integerlist[indexArray])  ## triggers: TypeError: list indices must be integers or slices, not list

I hope this helps. It even happened to me that I had to convert to a float array, otherwise the object would remain of the wrong type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!