python追加列表返回none

时间:2014-01-20 12:41:03

标签: python python-2.7

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

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

当我尝试在日期列表中使用它时,问题似乎与append和insert函数返回None有关

1 个答案:

答案 0 :(得分:4)

您忘记致电 list()类型:

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

您的异常(在评论中发布)显示您尝试在类型对象上调用.append

>>> 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(())

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

list_of_tuples = []