国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python 中IndexError: list assignment index out of range 錯(cuò)誤解決

這篇具有很好參考價(jià)值的文章主要介紹了Python 中IndexError: list assignment index out of range 錯(cuò)誤解決。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。


在 Python 中,當(dāng)您嘗試訪問甚至不存在的列表的索引時(shí),會(huì)引發(fā) IndexError: list assignment index out of range。 索引是可迭代對(duì)象(如字符串、列表或數(shù)組)中值的位置。

在本文中,我們將學(xué)習(xí)如何修復(fù) Python 中的 Index Error list assignment index out-of-range 錯(cuò)誤。


Python IndexError:列表分配索引超出范圍

讓我們看一個(gè)錯(cuò)誤的例子來理解和解決它。

代碼示例:

# error program --> IndexError: list assignment index out of range
i = [7,9,8,3,7,0]  # index range (0-5)
j = [1,2,3]        # index range (0-3)

print(i,"\n",j)
print(f"\nLength of i = {len(i)}\nLength of j = {len(j)}" )

print(f"\nValue at index {1} of list i and j are {i[1]} and {j[1]}")
print(f"\nValue at index {3} of list i and j are {i[3]} and {j[3]}") # error because index 3 isn't available in list j

輸出:

Python 中IndexError: list assignment index out of range 錯(cuò)誤解決,編程,Python 實(shí)用技巧,python,開發(fā)語言

上面代碼中 IndexError: list assignment index out of range 背后的原因是我們?cè)噲D訪問索引 3 處的值,這在列表 j 中不可用。


修復(fù) Python 中的 IndexError: list assignment index out of range

要修復(fù)此錯(cuò)誤,我們需要調(diào)整此案例列表中可迭代對(duì)象的索引。 假設(shè)我們有兩個(gè)列表,你想用列表 b 替換列表 a。

代碼示例:

a = [1,2,3,4,5,6]
b = []
k = 0

for l in a:
    b[k] = l # indexError --> because the length of b is 0
    k += 1

print(f"{a}\n{a}")

輸出:

IndexError: list assignment index out of range

您不能為列表 b 賦值,因?yàn)樗拈L(zhǎng)度為 0,并且您試圖在第 k 個(gè)索引 b[k] = I 處添加值,因此它會(huì)引發(fā)索引錯(cuò)誤。 您可以使用 append()insert() 修復(fù)它。


修復(fù) IndexError: list assignment index out of range 使用 append() 函數(shù)

append() 函數(shù)在列表末尾添加項(xiàng)目(值、字符串、對(duì)象等)。 這很有幫助,因?yàn)槟槐靥幚硭饕龁栴}。

代碼示例:

a = [1,2,3,4,5,6]
b = []
k = 0

for l in a:
   # use append to add values at the end of the list
    j.append(l)
    k += 1

print(f"List a: {a}\nList b: {a}")

輸出:

List a: [1, 2, 3, 4, 5, 6]
List b: [1, 2, 3, 4, 5, 6]

修復(fù) IndexError: list assignment index out of range 使用 insert() 函數(shù)

insert() 函數(shù)可以直接將值插入到列表中的第 k 個(gè)位置。 它有兩個(gè)參數(shù),insert(index, value)。

代碼示例:

a = [1, 2, 3, 5, 8, 13]
b = []
k = 0

for l in a:
    # use insert to replace list a into b
    j.insert(k, l)
    k += 1

print(f"List a: {a}\nList b: {a}")

輸出:

List a: [1, 2, 3, 4, 5, 6]
List b: [1, 2, 3, 4, 5, 6]

除了上述兩種解決方案之外,如果你想像對(duì)待其他語言中的普通數(shù)組一樣對(duì)待 Python 列表,你可以使用 None 值預(yù)定義你的列表大小。

代碼示例:

a = [1,2,3,4,5,6]
b = [None] * len(i)

print(f'Length of a: {len(a)}')
print(f'Length of b: {len(b)}')

print(f"\n{a}\n{b}")

輸出:

Length of a: 6
Length of b: 6

[1, 2, 3, 4, 5, 6]
[None, None, None, None, None, None]

一旦你用虛擬值 None 定義了你的列表,你就可以相應(yīng)地使用它。


總結(jié)

可能有更多的手動(dòng)技術(shù)和邏輯來處理 IndexError:Python 中的列表分配索引超出范圍。 本文概述了兩個(gè)常見的列表函數(shù),它們可以幫助我們?cè)谔鎿Q兩個(gè)列表時(shí)幫助我們處理 Python 中的索引錯(cuò)誤。

我們還討論了預(yù)定義列表并將其視為類似于其他編程語言數(shù)組的數(shù)組的替代解決方案。文章來源地址http://www.zghlxwxcb.cn/news/detail-735017.html

到了這里,關(guān)于Python 中IndexError: list assignment index out of range 錯(cuò)誤解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 關(guān)于Pyinstaller在打包Streamlit程序時(shí)遇到的IndexError:tuple index out of range

    關(guān)于Pyinstaller在打包Streamlit程序時(shí)遇到的IndexError:tuple index out of range

    如題,在使用Pyinstaller庫打包過程中,如果遇到 IndexError:tuple index out of range ,不必驚慌,本質(zhì)上是庫函數(shù)在傳參過程中出現(xiàn)異常 下面是解決方案: 找到 ..envssteamlitlibdis.py 這個(gè)文件。 如果你是用的虛擬環(huán)境,比如conda,那這個(gè)文件位于anaconda的安裝目錄 \\\"E:SOFTWAREANACONDAenv

    2024年03月11日
    瀏覽(27)
  • IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)錯(cuò)誤解決辦法

    在使用pytorch交叉熵?fù)p失函數(shù)loss = nn.CrossEntropyLos(v, targets)時(shí)發(fā)現(xiàn)報(bào)錯(cuò) IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)。 排查后發(fā)現(xiàn)模型預(yù)測(cè)值V在訓(xùn)練階段最后一個(gè)step時(shí),tensor維度從二維變成一維,導(dǎo)致出錯(cuò)。 例:訓(xùn)練集個(gè)數(shù)81,bs設(shè)置為4時(shí),最后一個(gè)step 只剩

    2024年02月10日
    瀏覽(21)
  • Stable Diffusion 圖生圖+ControlNet list index out of range

    Stable Diffusion 圖生圖+ControlNet list index out of range

    在webui1.5中用圖生圖+ControlNet批量處理圖片的時(shí)候報(bào)錯(cuò): controlnet indexError: list index out of range 解決方法: 在controlNet的設(shè)置頁中勾選不輸出檢測(cè)圖即可。 參考:https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/12286

    2024年02月07日
    瀏覽(28)
  • Python報(bào)錯(cuò):IndexError: index 0 is out of bounds for axis 0 with size 0

    Python報(bào)錯(cuò): 原因: 索引超出了列表的長(zhǎng)度。 eg1: eg2: 解決方法: 檢查是自己的索引錯(cuò)了, 還是數(shù)組長(zhǎng)度定義錯(cuò)了。

    2024年02月12日
    瀏覽(27)
  • 【Python】成功解決IndexError: index 1256 is out of bounds for axis 0 with size 629

    【Python】成功解決IndexError: index 1256 is out of bounds for axis 0 with size 629

    【Python】成功解決IndexError: index 1256 is out of bounds for axis 0 with size 629 ?? 個(gè)人主頁:高斯小哥 ?? 高質(zhì)量專欄:Matplotlib之旅:零基礎(chǔ)精通數(shù)據(jù)可視化、Python基礎(chǔ)【高質(zhì)量合集】、PyTorch零基礎(chǔ)入門教程?? 希望得到您的訂閱和支持~ ?? 創(chuàng)作高質(zhì)量博文(平均質(zhì)量分92+),分享更多

    2024年04月12日
    瀏覽(23)
  • 已解決IndexError: positional indexers are out-of-bounds

    已解決IndexError: positional indexers are out-of-bounds

    已解決IndexError: positional indexers are out-of-bounds 粉絲群里面的一個(gè)小伙伴遇到問題跑來私信我,想用pandas,但是發(fā)生了報(bào)錯(cuò)(當(dāng)時(shí)他心里瞬間涼了一大截,跑來找我求助,然后順利幫助他解決了,順便記錄一下希望可以幫助到更多遇到這個(gè)bug不會(huì)解決的小伙伴),報(bào)錯(cuò)代碼如下

    2024年02月05日
    瀏覽(20)
  • java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    ????????該錯(cuò)誤表示字符串的索引超出了 范圍 。這種錯(cuò)誤通常發(fā)生在嘗試訪問 字符串中不存在的索引 位置的時(shí)候。 ????????這個(gè)錯(cuò)誤可能有多種原因,具體取決于代碼邏輯和使用字符串的方式。以下是一些常見的導(dǎo)致此錯(cuò)誤的情況以及相應(yīng)的解決方法: 一、空字符

    2024年02月12日
    瀏覽(19)
  • 【nvm安裝npm出錯(cuò)】panic: runtime error: index out of range [3] with length 3

    【nvm安裝npm出錯(cuò)】panic: runtime error: index out of range [3] with length 3

    我執(zhí)行的命令是: npm install latest ,但是出現(xiàn)以下報(bào)錯(cuò): 升級(jí) nvm 至 1.1.11 。下載鏈接: https://github.com/coreybutler/nvm-windows/releases/download/1.1.11/nvm-update.zip 但是沒解決。 chatgpt 其實(shí)解決了的,但是我一開始不相信: 搜索 nvm ,找到作者的倉庫: https://github.com/coreybutler/nvm-windows 然

    2024年02月14日
    瀏覽(26)
  • nvm 安裝 Node 報(bào)錯(cuò):panic: runtime error: index out of range [3] with length 3

    nvm 安裝 Node 報(bào)錯(cuò):panic: runtime error: index out of range [3] with length 3

    最近在搞 TypeScript ,然后想著品嘗一下 pnpm ,但是 pnmp 8.x 最低需要 Node 16.x ,但是電腦上暫時(shí)還沒有該版本,通過 nvm list available 命令查看可用的 Node 版本: 既然有最高版本,那肯定直接上最高版本: 然后就報(bào)錯(cuò)了,錯(cuò)誤信息如下: 出問題果斷 Github 上去搜( 體會(huì)到了開源

    2024年02月16日
    瀏覽(66)
  • MybatisPlus的分頁插件自動(dòng)優(yōu)化LeftJoin語句導(dǎo)致參數(shù)不匹配
java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).

    MybatisPlus的分頁插件自動(dòng)優(yōu)化LeftJoin語句導(dǎo)致參數(shù)不匹配 java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).

    首先會(huì)去檢查自己傳了哪些參數(shù)?是否都用上了?反復(fù)檢查,發(fā)現(xiàn)并沒有多余的參數(shù): controller里處理?xiàng)l件并傳參: ?SQL語句 ?小伙伴說可能是我這sql語句太混亂,甚至還 mybatis和mybatisplus同時(shí)使用了?。?! ?遇事不決,查網(wǎng)友經(jīng)驗(yàn):(9條消息) MyBatis-Plus使用分頁插件報(bào)錯(cuò),或數(shù)

    2024年02月11日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包