我们假设我有一份清单。
mylistOfLists = [[1, 2], [3, 4], [5, 6], [7, 8]]
获得此结果的python中最优雅的方法是什么?
myCombinedList = [1, 2, 3, 4, 5, 6]
感谢您的任何建议!
答案 0 :(得分:2)
myCombinedList = []
[myCombinedList.extend(inner) for inner in mylistOfLists]
或者:
import itertools
myCombinedIterable = itertools.chain.from_iterable(mylistOfLists)
myCombinedList = list(myCombinedIterable)
答案 1 :(得分:0)
res=[]
for item in mylistOfList:
res+=item