![](/img/trans.png)
[英]Python: TypeError: 'int' object does not support item assignment for MinMaxScaler
[英]TypeError: 'int' object does not support item assignment - new to Python
嘗試在書中做一些問題,但我仍然堅持使用數組。 我收到此錯誤:
count[i] = x
TypeError: 'int' object does not support item assignment
我的代碼:
import pylab
count = 0
for i in range (0,6):
x = 180
while x < 300:
count[i] = x
x = x + 20
print count
基本上,我需要使用循環來存儲增加的值,直到它們在數組中達到300。 我不確定為什么會收到該錯誤。
我可能措辭很差。 我需要使用循環來制作6個值(從200、220、240 ...到300),並將它們存儲在數組中。
count
是一個整數。 整數沒有索引,只有列表或元組(或基於它們的對象)有索引。
您要使用列表,然后將值附加到它:
count = []
for i in range (0,6):
x = 180
while x < 300:
count.append(x)
x = x + 20
print count
您已將count
定義為一個int
數,並且如錯誤所示( 'int' object does not support item assignment
),您需要一個列表,:
count = []
並使用append()
函數將數字附加到列表中:
for i in range (0,6):
x = 180
while x < 300:
count.append(x)
x = x + 20
print count
您不需要while循環,可以將范圍與開始,停止,步驟一起使用:
count = []
for i in xrange (6):
for x in xrange(180,300,20):
count.append(x)
print count
或結合使用范圍:
count = []
for i in xrange(6):
count.extend(range(180,300,20))
或者簡單地:
count range(180,300,20) * 6 `
使用字典
count = {}
for i in range (0,6):
x = 180
while x < 300:
count[i] = x
x = x + 20
print count
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.