python tuple 使用踩坑

tuple使用小提示,不能像list一樣直接訪問其中的元素或者進行修改,需要轉化成list形式進行修改操作


t=("zhaoxiaorong",1,2,3)
tlist=list(t)
print(tlist)
t1=tlist[0]
t1=str.replace(t1,'o','i')
print(t1)
tlist[0]=t1
t=tuple(tlist)
print(t)

#直接訪問修改會出錯
# t[0]=str.replace(t[0],'o','i')
#TypeError: 'tuple' object does not support item assignment
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章