- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtraversal.py
More file actions
Latest commit
34 lines (31 loc) · 765 Bytes
/
Copy pathtraversal.py
File metadata and controls
34 lines (31 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
'''
from collections import Iterable
def flat(items, L=None):
if L is None:
L = []
for item in items:
if isinstance(item, Iterable):
return flat(item, L)
else:
L.append(item)
return L
if __name__ == "__main__":
items = [1, 2, [3, 4, [5, 6, 7], 8, 9]]
new_items = flat(items)
print(new_items)
'''
fromcollectionsimportIterable
defflat(items, ignore_types=(str, bytes)):
'''
http://python3-cookbook.readthedocs.io/zh_CN/latest/c04/p14_flattening_nested_sequence.html
'''
foriteminitems:
ifisinstance(item, Iterable) andnotisinstance(item, ignore_types):
yieldfromflat(item)
else:
yielditem
if__name__=="__main__":
items= [1, 2, [3, 4, [5, 6, 7], 8], 9]
foriinflat(items):
print(i)