将多维数据转化为单维数组

l = [1,2,3,[4,5,6,[7,8], [9, 10]], [11, 12]]

def plat(lst):
    for i in lst:
        if isinstance(i, Iterable):
            yield from plat(i)
        else:
            yield i

s = list(plat(l))
print(s)   #  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

你可能感兴趣的:(将多维数据转化为单维数组)