文章詳情頁
Python修改列表值問題解決方案
瀏覽:67日期:2022-08-03 15:46:59
由于慣性思維,導(dǎo)致使用for循環(huán)修改列表中的值出現(xiàn)問題
首次嘗試:
def make_great(original): for magician in original: magician = 'the Great ' + magicianmagicians = ['david', 'tom', 'jimmy']make_great(magicians)show_magicians(magicians)
運(yùn)行結(jié)果
顯然列表中的值并沒有改變。
思考:for語句定義一個變量進(jìn)行遍歷,但只是訪問當(dāng)前值。操作列表中的值正確方法是使用下標(biāo)。
修改后:
def make_great(original): j = len(original) for i in range(0, j): original[i] = 'the Great ' + original[i]
運(yùn)行結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP基礎(chǔ)知識VBScript基本元素講解2. Python requests庫參數(shù)提交的注意事項(xiàng)總結(jié)3. IntelliJ IDEA導(dǎo)入jar包的方法4. ajax請求添加自定義header參數(shù)代碼5. Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動庫6. vue-electron中修改表格內(nèi)容并修改樣式7. 詳談ajax返回數(shù)據(jù)成功 卻進(jìn)入error的方法8. 使用Python和百度語音識別生成視頻字幕的實(shí)現(xiàn)9. 使用python 計算百分位數(shù)實(shí)現(xiàn)數(shù)據(jù)分箱代碼10. python操作mysql、excel、pdf的示例
排行榜
