0

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

7
  • 4
    This code is not enough to diagnose the problem. Most likely problem is that ListData is an integer and not an array or that ListData[i] is an integer and not an array. Commented Apr 24, 2012 at 15:04
  • Is your ListData a list of lists or just a simple list? If it is a simple list, ListData[i][0] is incorrect. Commented Apr 24, 2012 at 15:04
  • What does ListData look like? Your code is assuming ListData contains sequences of integers, but your error message suggests it may contain just integers. Commented Apr 24, 2012 at 15:06
  • 1
    @Jules: It can't be an integer, or this code would fail on the line before when len(ListData) is called. So this code is perfectly enough to diagnose the problem. All the data is there. Commented Apr 24, 2012 at 15:06
  • 'ListData' contains values taken in from a text file that are ints and strings. i have the data being split every time a comma is come across. the data is just appended onto the end of the list then. Commented Apr 24, 2012 at 15:14

2 Answers 2

6

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]
Sign up to request clarification or add additional context in comments.

2 Comments

You could write int(item/max_val) as item // max_val
@Daenyth: It's not quite the same: 2//3.0 returns 0.0, so if max_val is a float, you'd get a list of floats, not of ints.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.