尝试在Python中的列表中使用'pop'方法时出错 [英] Error when attempting to use the 'pop' method with a list in Python

查看:40
本文介绍了尝试在Python中的列表中使用'pop'方法时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的过程,该过程只保留一个输入列表(包括嵌套列表).

I'm trying to code a simple procedure, that simply reserves an input list (including nested lists).

过程本身-

def is_list(p):
    return isinstance(p, list)

def deep_reverse(input):
    result = []
    if len(input) == 0:
        return []
    for element in input:
        if is_list(element) == True:
            deep_reverse(element)
        else:
            appended = input.pop
            result.append(appended)
            print result
    return result

我要输入的内容以执行该过程-

What I'm inputting to execute the procedure -

p = [1, [2, 3, [4, [5, 6]]]]
print deep_reverse(p)
print p

结果-

[built-in method pop of list object at 0xb752b0cc]
[built-in method pop of list object at 0xb7529c6c]
[built-in method pop of list object at 0xb7529cec]
[built-in method pop of list object at 0xb7529ecc]
[built-in method pop of list object at 0xb7529ecc]
[1, [2, 3, [4, [5, 6]]]]

我在做什么错了?

推荐答案

.pop 是一种方法.您必须调用它:

.pop is a method. You have to call it:

appended = input.pop()

这将从 input 中获取 last 元素.如果要使用 first 元素,请为其提供一个参数:

This will take the last element from input. If you want to take the first element, give it an argument:

appended = input.pop(0)

这篇关于尝试在Python中的列表中使用'pop'方法时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆