Update some snippets
This commit is contained in:
@ -1,27 +1,30 @@
|
||||
---
|
||||
title: deep_flatten
|
||||
tags: list
|
||||
tags: list,recursion,intermediate
|
||||
---
|
||||
|
||||
Deep flattens a list.
|
||||
|
||||
Use recursion. Use `list.extend()` with an empty list (`result`) and the spread function to flatten a list. Recursively flatten each element that is a list.
|
||||
Use recursion.
|
||||
Define a function, `spread`, that uses either `list.extend()` or `list.append()` on each element in a list to flatten it.
|
||||
Use `list.extend()` with an empty list and the `spread` function to flatten a list.
|
||||
Recursively flatten each element that is a list.
|
||||
|
||||
```py
|
||||
def spread(arg):
|
||||
ret = []
|
||||
for i in arg:
|
||||
if isinstance(i, list):
|
||||
ret.extend(i)
|
||||
else:
|
||||
ret.append(i)
|
||||
return ret
|
||||
|
||||
ret = []
|
||||
for i in arg:
|
||||
if isinstance(i, list):
|
||||
ret.extend(i)
|
||||
else:
|
||||
ret.append(i)
|
||||
return ret
|
||||
|
||||
def deep_flatten(lst):
|
||||
result = []
|
||||
result.extend(
|
||||
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))
|
||||
return result
|
||||
result = []
|
||||
result.extend(
|
||||
spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst))))
|
||||
return result
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
Reference in New Issue
Block a user