가 어떻게 변환 할 수 있습니다파이썬에서 중첩 목록을 병합하는 방법은 무엇입니까?
THIS = \
['logging',
['logging', 'loggers',
['logging', 'loggers', 'MYAPP',
['logging', 'loggers', 'MYAPP', '-handlers'],
['logging', 'loggers', 'MYAPP', 'propagate']
]
],
['logging', 'version']
]
로 : 파이썬에서
THAT = [
['logging'],
['logging', 'version'],
['logging', 'loggers'],
['logging', 'loggers', 'MYAPP'],
['logging', 'loggers', 'MYAPP', '-handlers'],
['logging', 'loggers', 'MYAPP', 'propagate']
]
(그냥 평평하게, 정렬 할 필요가 없습니다)?
많은 것을 시도했지만 해결 방법을 찾을 수 없습니다. 내가 트릭을 할 것으로 판단
def flatten(myList):
def inner(current, acc):
items = []
for x in myList:
if isinstance(x, list):
acc.extend(inner(x, []))
else:
items.append(x)
acc.extend(items)
return acc
return inner(myList, [])
을 : 재귀 함수 정말 빛나는 곳
http://stackoverflow.com/questions/952914/making-a-flat-list-out-of- : – dstromberg
[이 질문에] (http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python), 특히 [이 답변] (http : /stackoverflow.com/a/406822/1535629). – senshin
또한보십시오 : http://stackoverflow.com/questions/11377208/recursive-generator-for-flattening-nested-lists?rq=1 – wheaties