python 展开嵌套的序列
将一个多层嵌套的序列展开成一个单层列表
可以写一个包含yield from 语句的递归生成器来轻松解决这个问题。
from collections import Iterable def flatten(items, ignore_types=(str, bytes)): for x in items: if isinstance(x, Iterable) and not isinstance(x, ignore_types): yield from flatten(x) else: yield x items = [1, 2, [3, 4, [5, 6], 7], 8] # Produces 1 2 3 4 5 6 7 8 for x in flatten(items): print(x) bb=list(flatten(items)) print(bb)