python索引错误: 2 lists that are inside a list

标签 python list

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]

关于python索引错误: 2 lists that are inside a list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5863629/

相关文章:

python - 串联如何对列表中的列表对象起作用?

Python - 根据索引对数字列表进行排序

python - 错误 HTTP 错误 403 : SSL is required when pip installing MySQL-python

python - @ 运算符与 ndarray 或矩阵操作数出现新的意外操作数错误

python - 在 wxPython 中的两个面板之间传递元组

list - 在 Groovy 中将 map 列表转换为单个 map

Python:在 lxml.cssselect 中使用的 CSS 选择器

python - 如何在 azure 函数绑定(bind)中选择日期时间周数?

Python将字典的平面列表转换为层次结构树

java - 非法参数异常 : Could not parse [/24] - SubnetUtils