我有一份清单清单......
A = [ [[1,3]], [[3,5], [4,4], [[5,3]]] ]
以下函数输出[1, 3, 3, 5, 4, 4, 5, 3]
def flatten(a):
b = []
for c in a:
if isinstance(c, list):
b.extend(flatten(c))
else:
b.append(c)
return b
但是,我想在最后一级停止展平,以便获得[ [1,3], [3,5], [4,4], [5,3] ]
答案 0 :(得分:4)
您可以在展平前测试包含的列表:
def flatten(a):
b = []
for c in a:
if isinstance(c, list) and any(isinstance(i, list) for i in c):
b.extend(flatten(c))
else:
b.append(c)
return b
演示:
>>> def flatten(a):
... b = []
... for c in a:
... if isinstance(c, list) and any(isinstance(i, list) for i in c):
... b.extend(flatten(c))
... else:
... b.append(c)
... return b
...
>>> A = [ [[1,3]], [[3,5], [4,4], [[5,3]]] ]
>>> flatten(A)
[[1, 3], [3, 5], [4, 4], [5, 3]]
这试图在这种情况下尽可能高效; any()
只需要在找到列表之前进行测试,而不是所有元素。
答案 1 :(得分:2)
A = [ [[1,3]], [[3,5], [4,4], [[5,3]]] ]
print [child[0] if isinstance(child[0], list) else child for item in A for child in item]
输出
[[1, 3], [3, 5], [4, 4], [5, 3]]
注意:此解决方案仅适用于此问题。这不是通用的列表展平解决方案。
同样的想法,itertools.chain
from itertools import chain
print [item[0] if isinstance(child[0], list) else item for item in chain(*A)]
<强>输出强>
[[1, 3], [3, 5], [4, 4], [5, 3]]