开发者

python index error: 2 lists that are inside a list

开发者 https://www.devze.com 2023-03-02 15:03 出处:网络
def main(): L=[] list1=[5,1,3] list2=[4,6,2] L.append(lis开发者_C百科t1) L.append(list2) f(L) def f(L):
def main():
    L=[]
    list1=[5,1,3]
    list2=[4,6,2]
    L.append(lis开发者_C百科t1)
    L.append(list2)
    f(L)

def f(L):
    for i in range(6)
         print L[i]



IndexError: list index out of range


You're just appending the lists onto L so you get something like [[5, 1, 3], [4, 6, 2]]. You need to use extend like so:

L.extend(list1)
L.extend(list2)
print L      # [5, 1, 3, 4, 6, 2]


Appending two items to an empty list makes a 2-element list. Perhaps you wanted L.extend() instead?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号