可能的重复:
扁平化 Python 中的浅表列表
假设我有一个列表列表。
mylistOfLists = [[1, 2], [3, 4], [5, 6], [7, 8]]
python中获得这个结果的最优雅的方法是什么?
myCombinedList = [1, 2, 3, 4, 5, 6]
感谢您的任何建议!
可能的重复:
扁平化 Python 中的浅表列表
假设我有一个列表列表。
mylistOfLists = [[1, 2], [3, 4], [5, 6], [7, 8]]
python中获得这个结果的最优雅的方法是什么?
myCombinedList = [1, 2, 3, 4, 5, 6]
感谢您的任何建议!
myCombinedList = []
[myCombinedList.extend(inner) for inner in mylistOfLists]
或者:
import itertools
myCombinedIterable = itertools.chain.from_iterable(mylistOfLists)
myCombinedList = list(myCombinedIterable)
res=[]
for item in mylistOfList:
res+=item