Hiky
Hiky

Reputation: 81

python - 'int' object is not subscriptable

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.

Thanks

Upvotes: 0

Views: 6329

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336418

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

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].

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

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

Upvotes: 6

Andreas Florath
Andreas Florath

Reputation: 4612

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

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.

Upvotes: 2

Related Questions