标签:cto isa 循环 als core add def red use
# utils/core.py convert_legacy_filters_into_adhoc()
for filt in filter(lambda x: x is not None, fd[filters]): fd[‘adhoc_filters‘].append(to_adhoc(filt, ‘SIMPLE‘, clause))
list(map(str, [1,2,3,4,5,6,7,8]))
# 等效写法
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
# exampl_1
from functionls import reduce
def add(x, y):
return x + y
reduce(add, [1,3,5,7,9])
# exampl_2:把序列[1, 3, 5, 7, 9]
变换成整数13579
def fn(x,y):
return x*10 + y
reduce(fn, [1,3,5,7,9])
# exampl_2:字符串str
也是一个序列,将str转化为int
DIGITS = {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9} def str2int(s): def fn(x, y): return x * 10 + y def char2num(s): return DIGITS[s] return reduce(fn, map(char2num, s))
def char2num(s):
return DIGITS[s]
#使用lambda简化内容
def str2int(s):
return reduce(lambda x, y: x * 10 + y, map(char2num, s))
# 默认升序排列
sorted([36, 5, -12, 9, -21])
# 按照绝对值排序
sorted([36, 5, -12, 9, -21])
# 默认按照ASCII的大小排序,‘Z’ < ‘a‘, 所以‘Z’排在前面sorted([‘bob‘, ‘about‘, ‘Zoo‘, ‘Credit‘])
# 实现忽略大小写的排序方法: 先将字符串全部转化为大写or小写sorted([‘bob‘, ‘about‘, ‘Zoo‘, ‘Credit‘], key=str.lower)
# 实现反向排序sorted([‘bob‘, ‘about‘, ‘Zoo‘, ‘Credit‘], key=str.lower, reverse=True)
# 实现L的按照名称排序
L = [(‘Bob‘, 75), (‘Adam‘, 92), (‘Bart‘, 66), (‘Lisa‘, 88)]
sorted(L, key=lambda x: x[0])
python|高级函数|filter|map|reduce|sorted
标签:cto isa 循环 als core add def red use
原文地址:https://www.cnblogs.com/bennyjane/p/12696939.html