python追加列表返回none
我有以下代码来生成元组列表:python追加列表返回none,python,python-2.7,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
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_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\u of tuples.append(tuple(temporary\u list))
您更愿意提供一个可运行的程序来重现问题吗?您的代码片段不会对我产生错误(当用['2014',01',20']
替换返回\u a\u list的函数时),错误消息也没有意义。您能否a)向我们提供异常的完整回溯,以及b)实际产生异常的代码示例?您提供的代码不会产生任何错误。请提供一个代码示例…@user2856110:descriptor?然后您没有调用list()
。
list_of_tuples = []