标签:turn 必须 collect inf 过滤 container python app highlight
1、集合的推导式
语法:[exp for item in collection if codition]
if codition - 可选
语法:{key_exp:value_exp for item in collection if codition}
语法:{exp for item in collection if codition}
2、多函数模式
函数列表,python中一切皆对象。
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
|
# 处理字符串 str_lst = [ ‘$1.123‘ , ‘ $1123.454‘ , ‘$899.12312‘ ] def remove_space(str): "" " remove space "" " str_no_space = str.replace( ‘ ‘ , ‘‘ ) return str_no_space def remove_dollar(str): "" " remove $ "" " if ‘$‘ in str: return str.replace( ‘$‘ , ‘‘ ) else : return str def clean_str_lst(str_lst, operations): "" " clean string list "" " result = [] for item in str_lst: for op in operations: item = op (item) result.append(item) return result clean_operations = [remove_space, remove_dollar] result = clean_str_lst(str_lst, clean_operations) print result |
执行结果:[‘1.123‘, ‘1123.454‘, ‘899.12312‘]
3、匿名函数lambda
1、函数式编程
2、map/reduce函数
lst = [a1, a2 ,a3, ......, an]
reduce(func(x,y), lst) = func(func(func(a1, a2), a3), ......, an)
3、filter函数
标签:turn 必须 collect inf 过滤 container python app highlight
原文地址:https://www.cnblogs.com/chengjian-physique/p/11909297.html