Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,7 +1,11 @@
>>> def flatten(lst):
return sum( ([x] if not isinstance(x, list) else flatten(x)
for x in lst), [] )
>>> 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
>>> lst = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
>>> flatten(lst)
>>> l = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
>>> flatten(l)
[1, 2, 3, 4, 5, 6, 7, 8]

View file

@ -7,6 +7,6 @@
yield x
>>>lst = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
>>>print list(flatten(lst))
>>> lst = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
>>> print list(flatten(lst))
[1, 2, 3, 4, 5, 6, 7, 8]