-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator_nested.py
39 lines (33 loc) · 1.3 KB
/
generator_nested.py
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
35
36
37
38
39
# example in book for list nested expansion
def flatten(nested):
#生成器加递归 解嵌套序列
try:
# considering str class can't be recurred
try:nested+""
except TypeError:pass
else: raise TypeError
for sublist in nested:
for element in flatten(sublist):
yield element
except TypeError:
yield nested
# inspired by the example in Internet
def unwrap(nested):
# 递归解嵌套序列
if isinstance(nested,list)==0:
return [nested] #!!!very import return a list
else:
return sum(map(unwrap, nested),[])
# def unwrap(nested): return sum(map(unwrap, nested),[]) if isinstance(nested,list) else [nested]
# from functools import reduce
# from operator import add
# def unwrap(nested):
# try:nested+[]
# except TypeError:return [nested] #!!!very import return a list
# else:return reduce(add,map(unwrap,nested),[])
a = [[1,2,3,[4,5]],[6,7],[8],"nine"]
print(list(flatten(a)))
print(unwrap(a))
#心得: + 可以用来检测变量的类型,很多地方可以代替isinstance() 例如: nested+""
#sum()函数黑科技可以用来展开nested列表,只能是列表字符串等不行,例如sum("abc","")不行
#生成器:使用yield关键字的函数