Python:将列表列表转换为元组的元组 [英] Python: converting list of lists to tuples of tuples
问题描述
Python新手!我需要帮助转换列表的元组元组列表。
我想调用append_as_tuples函数,但是每次我返回它时,它都会显示
它只能连接列表(不是元组)到列表中
以下是我到目前为止:
<$ p $ b $ def append_as_tuple(t,l):
'''将列表l转换为元组并将其附加到元组t中作为单个值'''
return t [ :] +(tuple(l),)
def convert_lists(lol):
t = []
如果输入(lol)== a或type(lol)==元组:
return [convert_lists(lol)for i] t
返回append_as_tuples(lol,a)
# - 测试工具#
a = [范围(5),范围(10,20),['hello','goodbye']]
打印
打印convert_lists(a)
打印convert_lists([])
转换 list_of_lists
给一个元组元组,使用
tuple_of_tuples = tup le(列表中的x的元组(x))
A Python newbie! I need help converting a list of lists tuples of tuples.
I want to call the append_as_tuples function, but every time I return it, it says
it can only concatenate lists (not tuples) to lists
Here is what I have so far:
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([])
To convert list_of_lists
to a tuple of tuples, use
tuple_of_tuples = tuple(tuple(x) for x in list_of_lists)
这篇关于Python:将列表列表转换为元组的元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!