Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,11 +1,7 @@
|
|||
>>> def flatten(lst, results=[]): # 'results' defaults to an empty list []
|
||||
for e in lst: # for each element 'e' in lst
|
||||
if type(e) is list: # if that element is a list, then
|
||||
flatten(e, results) # flatten that sublist, appending results to "results"
|
||||
else: # if element is not a list, then
|
||||
results.append(e) # insert a copy of it at the end of "results"
|
||||
return results
|
||||
>>> def flatten(lst):
|
||||
return sum( ([x] if not isinstance(x, list) else flatten(x)
|
||||
for x in lst), [] )
|
||||
|
||||
>>> l = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
|
||||
>>> flatten(l)
|
||||
>>> lst = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
|
||||
>>> flatten(lst)
|
||||
[1, 2, 3, 4, 5, 6, 7, 8]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue