代码片段记录13

1️⃣[flatten list]

对二维list进行展开。

1
2
3
4
5
6
7
8
list2d = [[1,2,3],[4,5,6], [7], [8,9]]
# ①
flatten = [l for list in list2d for l in list]
# ②
import itertools
merged = list(itertools.chain(*list2d))
# or
merged = list(itertools.chain.from_iterable(list2d))