重複の可能性:
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