小编典典

python追加列表不返回

python

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

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有关。


阅读 202

收藏
2021-01-20

共1个答案

小编典典

你忘了 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 = []
2021-01-20