Python高阶函数——map/reduce/filter/sorted
2017-01-20 12:44
846 查看
# ## 高阶函数 from functools import reduce print(abs(-10)) print(abs) # 输出<built-in function abs> # 函数本身可以赋值给变量 f = abs print(f) print(f(-19)) # #传入函数 # 将函数作为参数传入另一个函数 def add(x, y, f): return f(x) + f(y) print( add(-6, -7, abs) ) # 所谓高阶函数,就是让函数的参数能够接收别的函数 # #map()函数接收两个参数,一个是函数,另一个是Iterable(可迭代对象) # map() 函数同样返回一个Iterable存储结果 def f(x): return x * x r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9]) print( list(r) ) # 返回迭代对象的字符串形式 print( list(map(str, [1, 2, 3, 4, 5, 6, 7, 8])) ) # #reduce()函数接收两个参数,一个是函数,另一个是序列 # reduce()函数把结果继续和序列的下一个元素做累计计算 # 函数效果reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4) # 定义一个求和函数 def add(x, y): return x + y print( reduce(add, [1, 2, 3, 4, 5, 6, 7, 8, 9]) ) # 定义一个str2int函数 d = {'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 10 * x + y def char2int(s): return d[s] return reduce(fn, map(char2int, s)) print( str2int('95949748') ) # 定义首字母大写函数 def normalize(name_list): def fn(name): if not isinstance(name, str): raise TypeError('Wrong type!') return str(name[0]).upper() + str(name[1:]).lower() return map(fn, name_list) L1 = ['adam', 'LISA', 'barT'] print( list(normalize(L1)) ) # 定义一个求乘积函数 def product(number_list): return reduce(lambda x, y: x * y, number_list) print( product([1, 2, 3, 4, 5, 6]) ) # #filter()函数 # filter()也接收一个函数和一个序列。和map()不同的是, # filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。 # 定义一个函数 过滤偶数 删去奇数 def is_odd(n): return n % 2 == 1 print( list(filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 0])) ) # 定义一个函数 移除空字符串 def is_empty(s_list): return s_list and s_list.strip() print( list(filter(is_empty, ['A', 'C', 'E', 'G', None, 'J', ' '])) ) # # sort()函数 排序函数 a = [-30, -4, -5, 9, 6, 7] s = ['Z', 'a', 'c', 'F'] print( sorted(a, key=abs) # 按绝对值排序 ) print( sorted(a) # 按从小到大排序 ) print( sorted(a, reverse=True) # 从大到小排序 ) print( sorted(s) # 按ASCII的大小比较的,'Z' < 'a',大写字母Z会排在小写字母a的前面。 ) print( sorted(s, key=str.lower) # 按转为小写后的顺序来排。 ) L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)] # 按照名字排序 print( sorted(L, key=lambda x: x[0]) ) # 按照后面数字排序 print( sorted(L, key=lambda x: x[1]) )
相关文章推荐
- python——高阶函数——map reduce filter sorted
- Python 函数式编程--高阶函数Map、Reduce、Filter、Sorted
- Python3中的高阶函数(map,reduce,filter,sorted)
- Python学习2--高阶函数map、reduce、filter、sorted
- python 高阶函数 map reduce filter sorted 闭包 装饰器
- python学习(9)高阶函数map、reduce、filter、sorted
- python--函数式编程 (高阶函数(map , reduce ,filter,sorted),匿名函数(lambda))
- Python教程学习简记8--Python 高阶函数 map/reduce filter sorted
- Python进阶:函数式编程(高阶函数,map,reduce,filter,sorted,返回函数,匿名函数,偏函数)...啊啊啊
- python学习笔记,map/reduce,filter,sorted
- 不得不知的python高阶函数(Map、Reduce、Filter)
- python基础之map/reduce/filter/sorted
- Python学习 Day 5 高阶函数 map/reduce filter sorter 返回函数 匿名函数 装饰器 偏函数
- python:内建函数、range()、map()、filter()、reduce()、sorted()、集合的使用、functools中的函数使用、MD5加密
- 【转】python的内建函数:filter,map,reduce,lambda,sorted等
- python2,3中内建函数map,reduce,filter,sorted配合lambda
- Python中的map(),reduce(),filter(),sorted()函数知识点总结
- Java 高阶函数的简单使用:map,reduce,filter,sorted
- Python高阶函数:filter、map、reduce、lambda
- Python学习(八)——map、reduce、filter、sorted