## 问题描述

``````a = ['this', 'is', ['a', ['sample', 'of', ['nested', 'lists'], ','],
'enjoy', ['it', 'and'], 'have'], 'fun']``````

``````a = ['this', 'is', 'a', 'sample', 'of', 'nested', 'lists', ',',
'enjoy', 'it', 'and', 'have', 'fun']``````

## 解决方法

### 递归

``````def flat_list_rec(the_list, res=None):
if res is None:
res = []
for item in the_list:
if isinstance(item, list):
flat_list_rec(item, res)
else:
res.append(item)
return res``````

``````# before
a = ['this', 'is', ['a', ['sample', 'of', ['nested', 'lists'], ','],
'enjoy', ['it', 'and'], 'have'], 'fun']
# after
a = ['this', 'is', 'a', 'sample', 'of', 'nested', 'lists', ',',
'enjoy', 'it', 'and', 'have', 'fun']``````

### 循环

#### Version 1

``````def flat_list_v_1(the_list):
is_nested = True
before = the_list[:]
while is_nested:
is_nested = False
now = []
for item in before:
if isinstance(item, list):
now.extend(item)
is_nested = True
else:
now.append(item)
before = now
return before``````

``````# before
a = ['this', 'is', ['a', ['sample', 'of', ['nested', 'lists'], ','],
'enjoy', ['it', 'and'], 'have'], 'fun']
# after
a = ['this', 'is', 'a', 'sample', 'of', 'nested', 'lists', ',',
'enjoy', 'it', 'and', 'have', 'fun']``````

Version 1缺陷很明显，遍历次数即list的嵌套深度，且每次遍历都会访问所有非list元素。

#### Version 2

Version 1 的两层循环实际上可以缩减成一层循环。对list遍历时，一旦碰到元素是list，则将此list的元素全都加入到原list的末尾，并将此list从原list中删除。简单来说就是每当碰到嵌套的list，就解开一层嵌套并添加到末尾，而遍历过的元素就一定能够保证不再有嵌套的list，这样遍历到末尾时就把原list“压平”了。

``````def flat_list_v_2(the_list):
now = the_list[:]
for item in now:
if isinstance(item, list):
now.extend(item)
now.remove(item)
return now``````

``````# before
a = ['this', 'is', ['a', ['sample', 'of', ['nested', 'lists'], ','],
'enjoy', ['it', 'and'], 'have'], 'fun']
# after
a = ['this', 'is', 'fun', 'a', 'enjoy', 'have', 'sample', 'of', ',',
'it', 'and', 'nested', 'lists']``````

Version 2更简单，但会将原先的嵌套顺序打乱，可能不满足实际要求。

#### Version 3

Version 2是将嵌套的list解开一层嵌套后放到原list末尾，而实际上我们可以将list解开一层嵌套后直接放在原来的位置，即遍历到此元素发现是list，则解开此list，将其中的元素放到当前位置，再进行遍历。这样就能保证遍历处理的顺序和嵌套的顺序一致。

``````def flat_list_v_3(the_list):
now = the_list[:]
res = []
while now:
else:
``````# before