參考app
【https://blog.csdn.net/qq_44768814/article/details/88614393】spa
【IndexError: list assignment index out of range】.net
報錯緣由就是list的擴充 要使用append 或者 extend,而不能直接list[i]去添加。code
好比blog
# 錯誤的list擴充方式 會報錯 a=[] for i in range(0,5): a[i]=i # IndexError: list assignment index out of range # 正確的list擴充 append a=[] for i in range(0,5): a.append(i) # 輸出: a # [0, 1, 2, 3, 4] # 正確的list擴充 extend 用來把list1擴充到list2上 a=[] b=[[1,2,3],[11,22,33],[111,222,333]] for i in range(0,3): a.extend(b[i]) # 輸出 a # [1, 2, 3, 11, 22, 33, 111, 222, 333]
2、list的條件查找get
a=[1,2,3]class
a.index(3)qq