python - python程序中出现 "List Reversing"错误

标签 python list

大家好! 我是 python 编程的初学者。我正在编写一个 python 程序来反转给定的输入列表。下面是它的代码:

    L1=list(input("Enter the numbers of list to be reversed : "))
    L2=[]

    def rever(La,Lb):
     if len(Lb)==0: 
      return La                               
     else:
      return rever(La.append(Lb.pop(0)),Lb)

    print rever(L2,L1)

例如,如果我们输入,

    1,2,3

输出应该是,

    [3,2,1]

但这并没有发生。 Python 给出以下错误:

    Traceback (most recent call last):
      File "Q3.py", line 10, in <module>
        print rever(L2,L1)
      File "Q3.py", line 8, in rever
        return rever(La.append(Lb.pop(0)),Lb)
      File "Q3.py", line 8, in rever
        return rever(La.append(Lb.pop(0)),Lb)
    AttributeError: 'NoneType' object has no attribute 'append'

我不明白。请帮帮我!!

最佳答案

也许你应该看看这个,而不是弹出和追加

In [5]: L1=list(input("Enter the numbers of list to be reversed : "))
Enter the numbers of list to be reversed : 1,2,3,4,5

In [6]: L1
Out[6]: [1, 2, 3, 4, 5]

In [7]: L2 = L1[::-1]

In [8]: L2
Out[8]: [5, 4, 3, 2, 1]

关于python - python程序中出现 "List Reversing"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14974003/

相关文章:

python - Julia 中数组的逻辑运算

python - pygame错误: video system not initalized

list - 暂停和恢复对两个列表的迭代?

c# - WCF List<string> 序列化/反序列化错误

python - 如何在Python中的两个不同类之间进行运算符重载

python - 如何有效地构造一个要从命令行部分运行的 python 脚本?

python - 在 Pandas 数据框上应用正则表达式函数

python - 通过类属性访问的字典列表

r - 提取具有两个以上字符并匹配模式的列表元素

python - 如何在 Bottle 模板中输出未转义的 python 列表?