A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
7
Task/Flatten-a-list/Python/flatten-a-list-1.py
Normal file
7
Task/Flatten-a-list/Python/flatten-a-list-1.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
>>> def flatten(lst):
|
||||
return sum( ([x] if not isinstance(x, list) else flatten(x)
|
||||
for x in lst), [] )
|
||||
|
||||
>>> lst = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
|
||||
>>> flatten(lst)
|
||||
[1, 2, 3, 4, 5, 6, 7, 8]
|
||||
14
Task/Flatten-a-list/Python/flatten-a-list-2.py
Normal file
14
Task/Flatten-a-list/Python/flatten-a-list-2.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
>>> def flat(lst):
|
||||
i=0
|
||||
while i<len(lst):
|
||||
while True:
|
||||
try:
|
||||
lst[i:i+1] = lst[i]
|
||||
except (TypeError, IndexError):
|
||||
break
|
||||
i += 1
|
||||
|
||||
>>> lst = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
|
||||
>>> flat(lst)
|
||||
>>> lst
|
||||
[1, 2, 3, 4, 5, 6, 7, 8]
|
||||
12
Task/Flatten-a-list/Python/flatten-a-list-3.py
Normal file
12
Task/Flatten-a-list/Python/flatten-a-list-3.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
>>> def flatten(lst):
|
||||
for x in lst:
|
||||
if isinstance(x, list):
|
||||
for x in flatten(x):
|
||||
yield x
|
||||
else:
|
||||
yield x
|
||||
|
||||
|
||||
>>>lst = [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []]
|
||||
>>>print list(flatten(lst))
|
||||
[1, 2, 3, 4, 5, 6, 7, 8]
|
||||
Loading…
Add table
Add a link
Reference in a new issue