python 多层列表展开成单层列表

发布于 2020-04-11 22:40:10

多维列表展开成单层列表

a=[1,2,[3,4,[5,6],7],8,["python",6],9]
def function(lst):
    for i in lst:
        if type(i)==list:
            yield from function(i)
        else:
            yield i
print(list(function(a))) # [1, 2, 3, 4, 5, 6, 7, 8, 'python', 6, 9]
0 条评论

发布
问题