擅长:python、mysql、java
<p>这里的嵌套结构适合递归算法遍历:</p>
<pre><code>x = ['a', '1', ['c', 'd', ['e', ['f', '123']]]]
def recurse(y):
left,right = [],None
# Here we loop over the elements and whenever we encounter
# another list, we recurse.
for elem in y:
if isinstance(elem,list):
right = recurse(elem)
else:
left.append(elem)
# If there is no further nested list, return only the
# concatenated values, else return the concatenated values
# and the next list in the nest.
if right is None:
return ["".join(left)]
else:
return ["".join(left),right]
print recurse(x)
</code></pre>
<p>结果是:</p>
^{pr2}$