Python 类型错误:“int”对象不可订阅
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4060572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'int' object is unsubscriptable
提问by b8b8j
In python I get this error:
在 python 中,我收到此错误:
TypeError: 'int' object is unsubscriptable
This happens at the line:
这发生在这一行:
sectorcalc[i][2]= ((today[2]/yesterday[2])-1)
I couldn't find a good definition of unsubscriptable for python anywhere.
我在任何地方都找不到 python 不可订阅的好定义。
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
What does this error mean?
这个错误是什么意思?
采纳答案by pyfunc
The "[2]" in today[2] is called subscript.
今天[2]中的“[2]”称为下标。
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.
由于您收到错误 - 'int' 对象是不可订阅的。这意味着“今天”不是一个序列而是一个 int 类型的对象。
You will need to find / debug why "today" or "yesterday" is an int type object when you are expecting a sequence.
当您需要一个序列时,您将需要查找/调试为什么“今天”或“昨天”是一个 int 类型对象。
[Edit: to make it clear]
[编辑:说清楚]
Error can be in
错误可能在
- sectorcalc[i]
- today (Already proved is a list)
- yesterday
- 部门计算[i]
- 今天(已经证明是一个列表)
- 昨天
回答by duffymo
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 quoteForTodayor something more expressive? Same for yesterday. Dividing two dates as you do makes no sense to me.
是today = datetime.date.today()吗?为什么日期会突然引用引用?变量名是否应该quoteForToday更具有表现力?对于yesterday. 像你这样划分两个日期对我来说毫无意义。
Since this is a quote, would todayand yesterdayrefer 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.
既然这是一个quote, 会today和yesterday指的是不同日期的价格或费率吗?名字很重要 - 仔细选择它们。从现在起六个月后,您可能是必须保持这种状态的人,而且您也不会记住它们的含义。
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
回答by Eric Leschinski
How to reproduce that error:
如何重现该错误:
myint = 57
print myint[0]
The people who wrote the compiler said you can't do that in the following way:
编写编译器的人说你不能通过以下方式做到这一点:
TypeError: 'int' object is unsubscriptable
If you want to subscript something, use an array like this:
如果要下标,请使用如下数组:
myint = [ 57, 25 ]
print myint[0]
Which prints:
哪个打印:
57
Solution:
解决方案:
Either promote your int to a list or some other indexed type, or stop subscripting your int.
要么将您的 int 提升为列表或其他索引类型,要么停止为您的 int 添加下标。

