是什麼unsubscriptable
在類型錯誤的情況下平均爲:Python的unsubscriptable
TypeError: 'int' object is unsubscriptable
編輯: 導致這一現象的短代碼示例。
a=[[1,2],[5,3],5,[5,6],[2,2]]
for b in a:
print b[0]
> 1
> 5
> TypeError: 'int' object is unsubscriptable
是什麼unsubscriptable
在類型錯誤的情況下平均爲:Python的unsubscriptable
TypeError: 'int' object is unsubscriptable
編輯: 導致這一現象的短代碼示例。
a=[[1,2],[5,3],5,[5,6],[2,2]]
for b in a:
print b[0]
> 1
> 5
> TypeError: 'int' object is unsubscriptable
這意味着您已經嘗試將整數視爲數組。例如:
a = 1337
b = [1,3,3,7]
print b[0] # prints 1
print a[0] # raises your exception
您試圖尋找一個int數組下標:
>>> 1[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is unsubscriptable
也就是說,方括號[]
是下標運算符。如果你嘗試下標運算符應用於對象不支持它(如不執行__getitem__()
)。
在示例代碼的問題是,所述陣列的「a」中包含兩種不同類型的:它有4 2-元件列表和一個整數。然後,您將嘗試對「a」中的每個元素(包括整數元素)進行子腳本編寫。
換句話說,你的代碼是切實做好:
print [1,2][0]
print [5,3][0]
print 5[0]
print [5,6][0]
print [2,2][0]
這中間線的地方確實「5 [0]」是什麼,是產生錯誤。
請包括所產生的消息的代碼。請包括實際產生此錯誤消息的**最小**代碼示例。 – 2010-11-08 12:15:15
良好的開端。謝謝。步驟2.打印'b'的值。你可以把這個例子減少到只有一個具有這個問題的'b'的特定值。你能做下一步嗎? – 2010-11-08 15:22:49
@美國洛特 - 好現在應該更清楚。 – Theodor 2010-11-09 09:16:12