Jump to content

python: int object is not subscriptable

Go to solution Solved by Claryn,

ALRIGHTY

problem solved:
As a novice in Python, but quite intermediate in Java, I made the stupid mistake of thinking that Python would not look at numCoins here:

    numCoins = [20,10,5,1]
    coins = [0] *len(numbers)

    for i in range(len(coins)):
        coins[i] = numCoins

and treat it as a pointer. I ended up with a list that contained a duplicate of the list numCoins. Fucked me over big time. 


def num_coins(numbers):
    numCoins = [20,10,5,1]
    coins = [0] *len(numbers)

    for i in range(len(coins)):
        coins[i] = numCoins

    temp = 0
    for i in range(len(numbers)):
        rest = numbers[i]
        for j in range(4):
            x = int(coins[i][j])
            if ((rest >= x) and (rest != 0)):

(numbers is a list)

 

I get the following:

 x = int(coins[j])
TypeError: 'int' object is not subscriptable

 

The nested list is on the form list = [[20,10,5,1],[20,10,5,1,],...] 

I have tried to print coins[0][0] which gives 20 and no error. What is wrong with coins(i)[j] (forum editor won't let me put []  around i)  when i is and int in range of coins and j is an int in range of the nested list inside coins?

 

 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
https://linustechtips.com/topic/676943-python-int-object-is-not-subscriptable/
Share on other sites

Link to post
Share on other sites

So this creates a list of '0' depending how long you want it? then it adds numCoins to each list?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

My python is a little rusty but

for i in range(len(numbers)):

this is probably your problem. I think you meant to write len(coins) but got confused, or you forgot about it when you wrote

x = int(coins[i][j])

in the same loop. Since i is calculated based on the length of numbers rather than that of coins the interpreter is probably trying to access a part of the list that doesn't exist or is null.

coins[0][0]

gives you the correct result, but

coins[1][0]

probably won't.

 

You may want to initialize coins with a larger number than 0 by the way.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

What i don't understand is why you would fill the array with 0 then loop till the end when you already know the length as that is passed to the function.

 

def num_coins(number):
    numCoins = [20,10,5,1]
    coins=[]
    for i in range(number):
        coins.append(numCoins)
    for i in range(len(coins)):
        for j in range(4):
            print(coins[i][j])
        
num_coins(5)
#output
20
10
5
1
20
10
5
1

also what are you planning that if to do? rest would be equal to [20,10,5,1] and x would be a single int from that list. You can't answer is rest is > x.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

36 minutes ago, vorticalbox said:

So this creates a list of '0' depending how long you want it? then it adds numCoins to each list?

That is the intent, yes.

33 minutes ago, Sauron said:

My python is a little rusty but


for i in range(len(numbers)):

this is probably your problem. I think you meant to write len(coins) but got confused, or you forgot about it when you wrote


x = int(coins[i][j])

in the same loop. Since i is calculated based on the length of numbers rather than that of coins the interpreter is probably trying to access a part of the list that doesn't exist or is null.


coins[0][0]

gives you the correct result, but


coins[1][0]

probably won't.

 

You may want to initialize coins with a larger number than 0 by the way.

My Python is not very good. I am used to Java, where I dont have all these (in my mind) silly declerations of for loops and in my opinion poor handling of arrays/lists.

 

coins and numbers is the same length, so I dont see the difference in len(coins) and len(numbers). 

 

20 minutes ago, vorticalbox said:

What i don't understand is why you would fill the array with 0 then loop till the end when you already know the length as that is passed to the function.

 


def num_coins(number):
    numCoins = [20,10,5,1]
    coins=[]
    for i in range(number):
        coins.append(numCoins)
    for i in range(len(coins)):
        for j in range(4):
            print(coins[i][j])
        
num_coins(5)
#output
20
10
5
1
20
10
5
1

also what are you planning that if to do? rest would be equal to [20,10,5,1] and x would be a single int from that list. You can't answer is rest is > x.

 

What the if statement does is not relevant yet -since I never got to test it!!

The task is to get a list, say numbers = [12,23,34,45,56,67,78,89,90,98,87,65,54,43,21], then return how many coins you would need of 20,10,5,1 in order to get the whole sum.

So output would be: ([0,1,0,1], [2,0,0,1] ... )

 

What I currently have, and believe is correct, without any testing is:

 


            x = int(coins[i][j])
            if ((rest >= x) and (rest != 0)):
                temp = numbers[j]
                rest = rest%coins[i][j]
                coins[numCoins[j]] = int(temp/coins[i][j])

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

35 minutes ago, Claryn said:

coins and numbers is the same length, so I dont see the difference in len(coins) and len(numbers).

That's what I meant when I said I was rusty, I completely misread the declaration of coins :P

 

Scratch that, have you tried assigning the value of coins[j] to a variable and then typecasting it to an integer?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

30 minutes ago, Sauron said:

That's what I meant when I said I was rusty, I completely misread the declaration of coins :P

 

Scratch that, have you tried assigning the value of coins[j] to a variable and then typecasting it to an integer?

I thought was what I did with the declaration of x. 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

3 minutes ago, Claryn said:

I thought was what I did with the declaration of x. 

Ideally yes, but I'm not sure the typecast int() accepts list[x][y] as an argument.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

2 minutes ago, Sauron said:

Ideally yes, but I'm not sure the typecast int() accepts list[x][y] as an argument.

    x = int(coins[1][0])
    print(x)

 

worked correctly. Printed 20. 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

19 minutes ago, Claryn said:

    x = int(coins[1][0])
    print(x)

 

worked correctly. Printed 20. 

hmm. maybe I'm onto something - have you tried to print [0][1]?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

18 minutes ago, Claryn said:

That gave 10. So yes, it works correctly. 

Try to use a different variable from i, maybe the previous loop is messing with the value

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

Somehow I fixed that problem.. now I just have out of bounds exceptions.. unsure why due to Python's handling of lists.

Code now looks like this:


def num_coins(numbers):
    numCoins = [20,10,5,1]
    coins = [0] *len(numbers)

    for i in range(len(coins)):
        coins[i] = numCoins

    x = int(coins[0][3])
    print(x)
    temp = 0
    for i in range(len(coins)):
        rest = numbers[i]
        for j in range(len(coins[0])-1):
            x = int(coins[i][j])
            print("b")
            if ((rest >= x) and (rest != 0)):
                temp = numbers[j]
                rest = rest%coins[i][j]
                coins[numCoins[j]] = int(temp/coins[i][j])
                print("heo")

    return coins

 

I am out of bounds on line:


coins[numCoins[j]] = int(temp/coins[i][j])

When i = 1, j = 1 ... and I dont understand why.

 

(nevermind the printing of heo and b - just to check how many times my loops are running and if condition is true)

 

EDIT> Found the problem... I forgot to change coins[numCoins[j]] when I realized that I am just dealing with a 2d list :P

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

Update:
The item not subscriptable error magically disappeared.. now I am left with a int division/modulo by zero error. 
However, the code-block where the error is found, has no variables that would ever be 0.

 


           
            if ((rest >= x) and (rest != 0)):
                temp = numbers[j]
                rest = rest%coins[i][j]
                coins[i][j] = int(temp/coins[i][j])

 

 

it gives the error:

    rest = rest%coins[j]
ZeroDivisionError: integer division or modulo by zero

 

The if-statement specifically checks if rest is 0.. and I have printed the whole coins list earlier in the program, is it never has a 0. 

By doing some testing I found that it gives the error when the program has printed and confirmed rest = 34, coins[j] = 10. 

The answer should then be 4. There are no 0s in that calculation.

 

What is the problem here? Python is driving me nuts - I never had any of these sorts of errors in Java or Cpp 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

ALRIGHTY

problem solved:
As a novice in Python, but quite intermediate in Java, I made the stupid mistake of thinking that Python would not look at numCoins here:

    numCoins = [20,10,5,1]
    coins = [0] *len(numbers)

    for i in range(len(coins)):
        coins[i] = numCoins

and treat it as a pointer. I ended up with a list that contained a duplicate of the list numCoins. Fucked me over big time. 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×