关于python:为什么list.append()返回None?

Why does list.append() return None?

我正在尝试使用Python计算后缀表达式,但是它不起作用。 我认为这可能是与Python相关的问题。

有什么建议么?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)

大多数更改顺序/映射项的函数,方法的确会返回Nonelist.sortlist.appenddict.clear ...

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


方法append不返回任何内容:

1
2
3
>>> l=[]
>>> print l.append(2)
None

您不得写:

1
l = l.append(2)

但简单地说:

1
l.append(2)

在您的示例中,替换为:

1
a_list = a_list.append(r)

1
a_list.append(r)


对于追加时的返回数据,请使用:

1
2
b = []  
a = b.__add__(['your_data_here'])


append函数更改列表,并返回None。这是执行http://hg.python.org/cpython/file/aa3a7d5e0478/Objects/listobject.c#l791的代码

1
2
3
4
5
6
listappend(PyListObject *self, PyObject *v)
{
    if (app1(self, v) == 0)
        Py_RETURN_NONE;
    return NULL;
}

所以,当你说

1
a_list = a_list.append(r)

您实际上是为a_listNone分配的。因此,下次您引用a_list时,它不是指向列表,而是指向None。因此,正如其他人所建议的,改变

1
a_list = a_list.append(r)

1
a_list.append(r)


只是一个想法,而不是那些返回None的函数(操纵实际数据),它们应该什么也不返回。
然后用户至少会发现问题,因为它会抛出一个错误,指出一些分配错误!
评论您的想法!


列表方法可以分为两种类型:将列表变量改变为原位并返回None(字面意义)的方法;将列表保持不变并返回与列表相关的值的方法。

第一类:

1
2
3
4
5
6
append
extend
insert
remove
sort
reverse

第二类:

1
2
count
index

以下示例说明了这些差异。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
lstb=list('Albert')
lstc=list('Einstein')

lstd=lstb+lstc
lstb.extend(lstc)
# Now lstd and lstb are same
print(lstd)
print(lstb)

lstd.insert(6,'|')
# These list-methods modify the lists in place. But the returned
# value is None if successful except for methods like count, pop.
print(lstd)
lstd.remove('|')
print(lstd)

# The following return the None value
lstf=lstd.insert(6,'|')
# Here lstf is not a list.
# Such assignment is incorrect in practice.
# Instead use lstd itself which is what you want.
print(lstf)

lstb.reverse()
print(lstb)

lstb.sort()
print(lstb)

c=lstb.count('n')
print(c)

i=lstb.index('r')
print(i)

弹出方法可以同时做到。它使列表变异并返回一个值。

1
2
3
popped_up=lstc.pop()
print(popped_up)
print(lstc)


诸如list.append(),list.sort()之类的函数不会返回任何内容。
例如

1
2
def list_append(p):
    p+=[4]

函数list_append没有return语句。因此,当您运行以下语句时:

1
2
3
4
a=[1,2,3]
a=list_append(a)
print a
>>>None

但是,当您运行以下语句时:

1
2
3
4
a=[1,2,3]
list_append(a)
print a
>>>[1,2,3,4]

就是这样,希望它可以为您提供帮助。