为什么 list.append() 返回 None? [英] Why does list.append() return None?

查看:53
本文介绍了为什么 list.append() 返回 None?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 计算后缀表达式,但没有成功.我认为这可能是一个与 Python 相关的问题.

I am trying to calculate a postfix expression using Python, but it did not work. I think this is maybe a Python-related problem.

有什么建议吗?

expression = [12, 23, 3, '*', '+', 4, '-', 86, 2, '/', '+']

def add(a,b):
    return a + b
def multi(a,b):
    return a* b
def sub(a,b):
    return a - b
def div(a,b):
    return a/ b


def calc(opt,x,y):
    calculation  = {'+':lambda:add(x,y),
                     '*':lambda:multi(x,y),
                     '-':lambda:sub(x,y),
                     '/':lambda:div(x,y)}
    return calculation[opt]()



def eval_postfix(expression):
    a_list = []
    for one in expression:
        if type(one)==int:
            a_list.append(one)
        else:
            y=a_list.pop()
            x= a_list.pop()
            r = calc(one,x,y)
            a_list = a_list.append(r)
    return content

print eval_postfix(expression)

推荐答案

只需将 a_list = a_list.append(r) 替换为 a_list.append(r).

大多数函数,改变序列/映射项的方法确实返回None:list.sortlist.append, dict.clear ...

Most functions, methods that change the items of sequence/mapping does return None: list.sort, list.append, dict.clear ...

不直接相关,但请参阅为什么 list.sort() 不返回排序后的列表?.

Not directly related, but see Why doesn’t list.sort() return the sorted list?.

这篇关于为什么 list.append() 返回 None?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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