递归生成器

l=[['a','b'],[1,2],['Q','E']]
n=[1,2,3,4,5]


def flatten(nested):
    try:
        try:
            nested + " "
        except TypeError:
            pass
        else:
            raise TypeError
        for sublist in nested:
            for element in flatten(sublist):
                yield element

    except TypeError as e:
        yield nested

r=flatten(l)    
for i in r:
    print(i)

你可能感兴趣的:(递归生成器)