2014-01-20 81 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 = []