如何把嵌套的python list转成一个一维的python list

2024-12-04 16:52:16
推荐回答(2个)
回答(1):

def flattern(nested):
    result = []
    if isinstance(nested, list):
        for sublist in nested:
            result.extend(flattern(sublist))
     else:
         result.append(nested)
     return result

a = [[1,2],3,5,[[6,7],[8]]]
flattern(a) # [1,2,3,5,6,7,8]

回答(2):

list = [[1,2,3],[4,5,6]]
list = list[0]+list[1]