我有以下代码来生成元组列表:
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有关。
你忘了 叫 的list()类型:
list()
list_of_tuples = list() # ----------------^ You didn't do this.
您的异常(如评论中所述)表明您尝试调用.append类型对象:
.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 = []