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([])
最佳答案
要将 list_of_lists
转换为元组的元组,请使用
tuple_of_tuples = tuple(tuple(x) for x in list_of_lists)
关于Python:将列表列表转换为元组的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5506511/