簡體   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