简体   繁体   中英

Python: converting list of lists to tuples of tuples

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([])  

要将list_of_lists转换为元组元组,请使用

tuple_of_tuples = tuple(tuple(x) for x in list_of_lists)

there is a python built in function: list and tuple

list( the tuple)...to conver tuple to list tuple( the list )....to convert list to tuple

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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