2014-01-20 79 views
-3

我有以下的代码来生成元组的列表:蟒蛇追加列表回报没有

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 

的问题似乎涉及到了追加和插入函数返回None当我尝试使用它们的日期列表

+1

会发生什么:

>>> 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.append(元组(temporary_list))' – RickyA

+8

您是否更愿意提供可重现问题的可运行[SSCE](http://sscce.org/)? – bereal

+2

你的代码片段对我来说不会产生错误(当用'['2014','01','20']'替换'function_that_return_a_list'),错误信息也没有意义。你能a)向我们提供异常的* full *追踪和b)实际产生异常的代码示例? –

回答

4

你忘了呼叫list()类型:

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

你的异常(如张贴在评论)抄WS你试着拨打.append类型的对象,而不是上:当你做

list_of_tuples = []