Flatten a nested list · 중첩列表 평탄화 Flatten
Flatten a nested list
A list can hold integers and other lists, nested to any depth: [1, [2, 3], [4, [5, 6]]]. Return one flat list of the integers in order: [1, 2, 3, 4, 5, 6].
Recursion fits perfectly. Loop over each item: if it's a list, flatten it and add the results; otherwise it's an integer, so add it directly. Use isinstance(item, list) to tell them apart.
중첩된 목록 평탄화
목록은 정수 와 다른 목록을 포함할 수 있으며, 어떤 깊이까지나 중첩될 수 있습니다: [1, [2, 3], [4, [5, 6]]]. 정수를 순서대로 나열한 단일 평탄한 목록을 반환하십시오: [1, 2, 3, 4, 5, 6].
**재귀(recursion)**가 완벽하게 맞습니다. 각 항목을 순환while: 항목이 목록이면 그 자체를 평탄화하여 결과를 더하고, 그렇지 않으면 정수이므로 직접 더하십시오. 구별하기 위해 isinstance(item, list)을 사용하십시오.
Write flatten(nested): given a list that may contain nested lists of integers (any depth), return a single flat list of the integers, in order. Use recursion. · flatten(nested)를 작성하십시오: 정수 중첩列表(임의 깊이)가 포함된列表가 주어졌을 때, 순서를 유지한 단일 평탄화列表를 반환하십시오. 재귀(recursion)를 사용하십시오.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.