天宫鹤

Python递归式生成器

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:
        yield nested

 

posted on 2024-08-15 17:15  GoGrid  阅读(5)  评论(0编辑  收藏  举报

导航