[译] 如何将列表嵌套列表的情况转化成一维列表

解决方案:

可以使用包, itertools.chain()

import itertools

list2d = [[1,2,3], [4,5,6], [7], [8,9]]
merged = list(itertools.chain(*list2d))

也可以不使用解包的方法, 使用itertools.chain.from_iterable

merged = list(itertools.chain.from_iterable(list2d))

原文链接: https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists

posted @ 2021-10-20 18:07  bingo彬哥  阅读(117)  评论(0编辑  收藏  举报
本站总访问量