【发布时间】:2011-07-27 06:20:14
【问题描述】:
Python 新手!我需要帮助转换列表元组的列表。
我想调用 append_as_tuples 函数,但每次我返回它时,它都会说
它只能将列表(不是元组)连接到列表
这是我目前所拥有的:
def append_as_tuple(t, l):
''' Convert list l to a tuple and append it to tuple t as a single value '''
return t[:] + (tuple(l),)
def convert_lists(lol):
t = []
if type(lol) == a or type(lol) == tuple:
return [convert_lists(lol) for i in t]
return append_as_tuples(lol,a)
#- test harness#
a=[range(5), range(10,20), ['hello', 'goodbye']]
print a
print convert_lists(a)
print convert_lists([])
【问题讨论】:
-
您的
if type(lol) == a完全错误;它似乎起作用只是因为您的“测试工具”中有一个名为a的全局变量,而a不是type实例......所以测试将是False.如果没有全局a,它会引发异常。
标签: python list function tuples