假设没有像这样的“混合”列表,这似乎可行
[1,2,[3]]:
def get_inner(nested): if all(type(x) == list for x in nested): for x in nested: for y in get_inner(x): yield y else: yield nested
输出
list(get_inner(nested_list)):
[[1, 3, 4, 5], [1, 3, 8], [1, 7, 8], [6, 7, 8], [9]]
甚至更短,没有生成器,
sum用于合并结果列表:
def get_inner(nested): if all(type(x) == list for x in nested): return sum(map(get_inner, nested), []) return [nested]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)