2
def main():
L=[]
list1=[5,1,3]
list2=[4,6,2]
L.append(list1)
L.append(list2)
f(L)
def f(L):
for i in range(6)
print L[i]
IndexError: list index out of range
def main():
L=[]
list1=[5,1,3]
list2=[4,6,2]
L.append(list1)
L.append(list2)
f(L)
def f(L):
for i in range(6)
print L[i]
IndexError: list index out of range
你只是將列表附加到L上,所以你得到類似[[5, 1, 3], [4, 6, 2]]
的東西。你需要使用extend
像這樣:
L.extend(list1)
L.extend(list2)
print L # [5, 1, 3, 4, 6, 2]
將兩個項目追加到空列表將生成一個2元素列表。也許你想要L.extend()
而不是?