python附加列表返回无 [英] python append list return none

查看:56
本文介绍了python附加列表返回无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来生成元组列表:

list_of_tuples = list()对于 list_of_names 中的名称:临时列表 = [名称]date = function_that_return_a_list #like ['2014', '01', '20']临时列表 = 临时列表 + 日期打印临时列表 #返回正确的列表 [name, '2014', '01', '20']list_of_tuples.append(temporary_list) #crashs 错误消息TypeError: append() 只需要一个参数(0 给定)"打印清单

问题似乎与当我尝试在日期列表中使用它们时追加和插入函数返回 None 相关

解决方案

你忘记调用list()类型:

list_of_tuples = list()# ----------------^ 你没有这样做.

您的异常(如评论中发布的)表明您尝试在类型对象上调用 .append :

<预><代码>>>>list.append(())回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>类型错误:描述符追加"需要列表"对象但收到元组">>>列表().append(())

在任何情况下最好使用 [] 来生成一个空列表:

list_of_tuples = []

I have the following code to generate a list of tuples:

list_of_tuples = list()

for name in list_of_names:
    temporary_list = [name]
    date = function_that_return_a_list      #like ['2014', '01', '20']
    temporary_list = temporary_list + date
    print temporary_list    #returns the correct list of [name, '2014', '01', '20']
    list_of_tuples.append(temporary_list)     #crashes with the error message "TypeError: append() takes exactly one argument (0 given)"

print flist

The problem seem to be related to that the append and insert functions return None when I try and use them on the date list

解决方案

You forgot to call the list() type:

list_of_tuples = list()
#    ----------------^ You didn't do this.

Your exception (as posted in the comments) shows that you tried to call .append on the type object instead:

>>> list.append(())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor 'append' requires a 'list' object but received a 'tuple'
>>> list().append(())

Better use [] to produce an empty list in any case:

list_of_tuples = []

这篇关于python附加列表返回无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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