python - 'int'对象不可订阅

时间:2021-06-22 16:57:29

I am attempting a bit of knn classification. when i try to normalize the data in an array i keep getting the above error.

我正在尝试一些分类。当我尝试规范化数组中的数据时,我不断得到上述错误。

    norm_val = 100.00                                                              
    for i in range(0, len(ListData)):                                               
            ListData[i][0] = int(ListData[i][0]/max_val)

I'm getting the error on the last line which says, 'int' object is not subscriptable.

我在最后一行收到错误,说'int'对象不可订阅。

Thanks

谢谢

2 个解决方案

#1


6  

ListData appears to be a list of integers (or at least a list that also contains integers).

ListData似乎是一个整数列表(或者至少是一个包含整数的列表)。

Therefore, ListData[i] returns the ith integer of the list. And since there is no such thing as "the first element of an integer", so you get this error when trying to access ListData[i][0].

因此,ListData [i]返回列表的第i个整数。并且由于没有“整数的第一个元素”这样的东西,所以在尝试访问ListData [i] [0]时会出现此错误。

Aside from that, if you're aiming to divide all items of a list by max_val, you can simply use a list comprehension:

除此之外,如果你的目标是通过max_val划分列表中的所有项目,你可以简单地使用列表理解:

ListData = [int(item/max_val) for item in ListData]

#2


2  

ListData contains not only lists but also other objects which are not lists.

ListData不仅包含列表,还包含非列表的其他对象。

The following works:

以下作品:

ListData = [ [99, "Some thing"],
             [88, "Some other thing"] ]

The following does not:

以下不是:

ListData = [ 99,
             88 ]

It is not really clear what you want to do.

你想做什么并不是很清楚。

#1


6  

ListData appears to be a list of integers (or at least a list that also contains integers).

ListData似乎是一个整数列表(或者至少是一个包含整数的列表)。

Therefore, ListData[i] returns the ith integer of the list. And since there is no such thing as "the first element of an integer", so you get this error when trying to access ListData[i][0].

因此,ListData [i]返回列表的第i个整数。并且由于没有“整数的第一个元素”这样的东西,所以在尝试访问ListData [i] [0]时会出现此错误。

Aside from that, if you're aiming to divide all items of a list by max_val, you can simply use a list comprehension:

除此之外,如果你的目标是通过max_val划分列表中的所有项目,你可以简单地使用列表理解:

ListData = [int(item/max_val) for item in ListData]

#2


2  

ListData contains not only lists but also other objects which are not lists.

ListData不仅包含列表,还包含非列表的其他对象。

The following works:

以下作品:

ListData = [ [99, "Some thing"],
             [88, "Some other thing"] ]

The following does not:

以下不是:

ListData = [ 99,
             88 ]

It is not really clear what you want to do.

你想做什么并不是很清楚。