Python:reorder list v2.7 vs 3.2不同的结果

2024-04-25 19:40:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经研究了SOHow can I reorder a list in python?来寻找解决方案。然而,当我运行代码时,在一个Python shell中它可以工作,而在另一个shell中则不行。两者都是2.7版。(编辑:一个版本是2.7,另一个是3.2) 2.7版代码中我收到错误(3.2版)的部分如下:

for r in range(c):  # c = 3 as I make 3 changes in the list
    p[list_a[r]], p[list_b[r]] = p[list_b[r]], p[list_a[r]]    # IndexError: list index out of range
    # p = [1, 2, 3, 4, 5] # p = starting list
    # list_a = [1,1,4]
    # list_b = [2,3,0]

    # ** Alternatively (brings same result): **
    #temp = p[list_a[r]]    # IndexError: list index out of range
    #p[list_a[r]] = p[list_b[r]]
    #p[list_b[r]] = temp

为什么对于同一版本的Python我会得到不同的结果(工作还是不工作)?我如何解决索引器问题? 谢谢你的帮助。你知道吗

编辑:在线shell似乎是python3.2版本。所以这可能就是为什么那个版本给我一个错误信息的原因吗?你知道吗


Tags: of代码in版本编辑indexrangeshell