我有一個非結構化列表作爲輸入,我需要在對它們執行多個分析之前變平。一旦我得到每個輸入的結果,將它們放回到原始列表的相同結構中的最佳方式是什麼?基於Python中的模式將單個列表分成多個列表的最佳方式是什麼?
inputList = [["a", ["b","c","d"], [["e"]], "f"],["g"]]
flattenedList = myFlattenListFunction(inputList)
# a number of calculations based on the inputList
# ...
flattenedResults = [0, 1, 2, 3, 4, 5, 6, 7]
#What is the best solution to restructure the results to match the inputLists?
[[1, [2,3,4], [[5]], 6], [7]]
你能告訴我們更多關於計算的信息嗎?可能不能平坦化? – L3viathan
'chain'不會扁平化任意嵌套的列表(例如'[「e」]') – donkopotamus
@donkopotamus好,請記住,'chain.from_iterable([任何東西])'只會給你任何東西' –