繁体   English   中英

Python:将列表列表转换为元组元组

[英]Python: converting list of lists to tuples of tuples

一个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:list和tuple

list(the tuple)...将元组转换为列表元组(列表)....将列表转换为元组

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM