def flatten(l):
"""
Turns a nested graph of lists/tuples/other objects into a list of objects.
Parameters
----------
l : list/tuple/other objects
Might be nested.
Returns
-------
object
A flattened list of objects.
"""
if isinstance(l, (list, tuple, collections.ValuesView)):
rval = []
for elem in l:
if isinstance(elem, (list, tuple)):
rval.extend(flatten(elem))
else:
rval.append(elem)
else:
return [l]
return rval
评论列表
文章目录