tags:

views:

99

answers:

2

I am running into this problem - TypeError: 'int' object is unsubscriptable

This happens at the line: (more code below) sectorcalc[i][2]= ((today[2]/yesterday[2])-1)

But the object today[2] is , any ideas?

Also, I couldn't find a good definition of unsubscriptable for python anywhere? what does this actually mean the problem is?

Thanks in advance:

for quote in sector[singlestock]:
        i+=1
        if i < len(sector):
            if i==0:
                sectorcalc[i][0]= quote[0]
                sectorcalc[i][2]= 0
                sectorcalc[i][3]= 0
                sectorcalc[i][4]= 0
                sectorcalc[i][5]= 0
                sectorcalc[i][6]= 0
                sectorcalc[i][7]= 0
            else:                    
                yesterday = sector[singlestock-1][i]

                print yesterday                                

                today = quote

                print type(today[2])
                sectorcalc[i][2]= ((today[2]/yesterday[2])-1)
                sectorcalc[i][3]= (today[3]/yesterday[3])-1
                sectorcalc[i][4]= (today[4]/yesterday[4])-1
                sectorcalc[i][5]= (today[5]/yesterday[5])-1 
                sectorcalc[i][6]= (today[6]/yesterday[6])-1
                sectorcalc[i][7]= (today[7]/yesterday[7])-1
+4  A: 

The "[2]" in today[2] is called subscript.

This usage is possible only if "today" is a sequence type. Native sequence types - List, string, tuple etc

Since you are getting an error - 'int' object is unsubscriptable. It means that "today" is not a sequence but an int type object.

You will need to find / debug why "today" or "yesterday" is an int type object when you are expecting a sequence.

[Edit: to make it clear]

Error can be in

  1. sectorcalc[i]
  2. today (Already proved is a list)
  3. yesterday
pyfunc
Thanks for your help, but when I print today:['AMGN', datetime.datetime(2009, 1, 5, 0, 0), 59.219999999999999, 59.649999999999999, 58.0, 59.649999999999999, 10940100.0, 59.649999999999999] isn't that a sequence?
b8b8j
The line before the one that fails (`print type(today[2])`) obviously runs fine and subscripts `today`. So it must be subscriptable.
delnan
@b8b8j: error could be that "today" or "yesterday" is not a sequence. Error is in the line and you will need to determine which one is causing the error.
pyfunc
@delnan: from the look of it "yesterday" seems unsubscriptable.
pyfunc
Hmm... That makes sense but when I add this print type(yesterday)print yesterday[2], I get <type 'list'>, 17.79, and the error is in a line after this?
b8b8j
its sectorcalc, thanks for all the help
b8b8j
A: 

This is confusing to read:

today = quote 

Is today = datetime.date.today()? Why would a date suddenly refer to a quote? Should the variable name be quoteForToday or something more expressive? Same for yesterday. Dividing two dates as you do makes no sense to me.

Since this is a quote, would today and yesterday refer to prices or rates on different days? Names matter - choose them carefully. You might be the one who has to maintain this six months from now, and you won't remember what they mean, either.

Not that the code you wrote is valid, but I can't see why you wouldn't use a loop.

for j in range(2,7):
    sectorcalc[i][j] = (today[j]/yesteday[j])-1

instead of

        sectorcalc[i][2]= ((today[2]/yesterday[2])-1)
        sectorcalc[i][3]= (today[3]/yesterday[3])-1
        sectorcalc[i][4]= (today[4]/yesterday[4])-1
        sectorcalc[i][5]= (today[5]/yesterday[5])-1 
        sectorcalc[i][6]= (today[6]/yesterday[6])-1
        sectorcalc[i][7]= (today[7]/yesterday[7])-1 
duffymo
another alternative is to use slice assignment: `sectorcalc[i][2:7] = [(t / y) - 1 for t, y in zip(today[2:7], yesterday[2:7])]`, yet another alternative is to replace with a new list (though this will have a slightly different semantic): `sectorcalc[i] = [quote[0], 0, 0, 0, 0, 0, 0]`
Lie Ryan