TypeError系列之:TypeError: 'tuple' object does not support item assignment
出現(xiàn)這種原因的問題是:tuple元素只能讀,不可以寫。這是初學(xué)數(shù)據(jù)類型時(shí)容易忽略的問題。
而list類型可讀可寫
上代碼:
import numpy as npa = np.zeros([5,5])b = (1,2,1)c = [1,2,1]print(type(a)) print(type(b)) print(type(c))print(c[0]) c[0] = 2 print(b[0]) b[0] = 2 #報(bào)錯(cuò)輸出:
<class 'numpy.ndarray'> <class 'tuple'> <class 'list'> 1 1b[0] = 2 #報(bào)錯(cuò) TypeError: 'tuple' object does not support item assignment總結(jié):我們?cè)诼暶鲾?shù)組時(shí),有多種方式例:numpy ,直接賦值等,在直接賦值時(shí),如果用的是“()”,則默認(rèn)生成的類型為tuple;如果是"[? ]",則默認(rèn)為list類型;如果是“{? }”,則默認(rèn)為set類型。注,sei類型不支持索引,如果使用set[0]索引會(huì)報(bào)錯(cuò):TypeError: 'set' object does not support indexing。
我們可以通過 tuple(),list(),set() 函數(shù)進(jìn)行類型轉(zhuǎn)換,但注意,這不會(huì)改變本身的性質(zhì),必須用一個(gè)新變量去接收該函數(shù)的返回值!!!
tuple,list類型均不含shape屬性,調(diào)用(a.shape或a.shape())會(huì)報(bào)錯(cuò):AttributeError: 'xxx' object has no attribute 'shape'
?
總結(jié)
以上是生活随笔為你收集整理的TypeError系列之:TypeError: 'tuple' object does not support item assignment的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TypeError系列之:TypeErr
- 下一篇: UnicodeDecodeError: