python:内建高阶函数map、filter、sorted、reduce
满足下列条件之一的即为高阶函数
【1】 函数接受一个或多个函数作为参数传入
【2】 函数返回一个函数
内建的高阶函数:
1、map
map(func,iterables)
参数:func,函数
iterable,序列(可迭代对象)。可以有多个,此时以最短的序列的元素为标准,调用func函数做运算
作用:用iterable中的每一个元素作为参数,调用func
返回值:python2 返回的是一个列表。
python3 返回的是一个对象,相当于迭代器。可以用迭代器的next方法取值
可以用for循环访问,可以用list()转换成列表,
In [14]: def power_a_b(a, b):
...: return a ** b
...:
...:
...: for x in map(power_a_b,
...: [1, 2, 3, 4, 5, 6, 7, 8],
...: [4, 3, 2, 1]):
...: print(x)
...:
1
8
9
4
In [53]: r = map((lambda a,b:a**b),[1,2,3,4,5,6,7],[4,3,2,1])
In [54]: r
Out[54]: <map at 0x1d9a86c6d48>
In [55]: next(r)
Out[55]: 1
In [56]: next(r)
Out[56]: 8
In [57]: next(r)
Out[57]: 9
In [58]: next(r)
Out[58]: 4
In [59]: next(r,100)
Out[59]: 100
In [60]: next(r,100)
Out[60]: 100
#map应用,规范输出=============================================
""" 输入用户姓名(英文),随意输入.
输出时要求每个用户名的首字母大写
e.g:输入:["kake","ADEE"]
输出:["Kake","Adee"]"""
def func_titlename(s):
name = s.title()
return name
def input_name():
l = []
while True:
s = input("请输入人名(英文):")
if not s:
break
l.append(s)
return l
l = input_name()
print(list(map(func_titlename,l)))
#输出:
请输入人名(英文):ded
请输入人名(英文):degFWE
请输入人名(英文):GRGWja
请输入人名(英文):NUNO
请输入人名(英文):
['Ded', 'Degfwe', 'Grgwja', 'Nuno']
2、filter
filter(func,iterable)
参数:func,函数
iterable,可迭代对象 只有一个
作用:用func对iterable的每一个元素进行筛选,func返回False 丢弃,返回True 保留
返回值:python2 返回的是一个列表
python3返回的是一个filter对象,相当于迭代器,可以用迭代器的next方法取值
可以用for循环访问,可以用list()转换成列表,
def is_odd(x):
if x % 2 == 1:
return True
return False
# 生成1~10以内奇数的列表 # [1, 3, 5, 7, 9]
for x in filter(is_odd, range(1, 10)):
print(x)
L = [x for x in filter(is_odd, range(1, 10))]
print(L) # [1, 3, 5, 7, 9]
#=====================
In [71]: x = filter(is_odd,range(1,10))
In [72]: x
Out[72]: <filter at 0x1d9a86b64c8>
In [73]: next(x,100)
Out[73]: 1
In [74]:
In [74]: next(x,100)
Out[74]: 3
In [75]: next(x,100)
Out[75]: 5
In [76]: next(x,100)
Out[76]: 7
In [77]: next(x,100)
Out[77]: 9
In [78]: next(x,100)
Out[78]: 100
In [79]: next(x,100)
Out[79]: 100
def is_prime(x):
if x < 2:
return False
# 排序法
for i in range(2, x):
if x % i == 0:
return False
return True
for x in filter(is_prime, range(20, 30)):
print(x)
3、sorted
sorted(iterable,key=None,reverse=False)
参数:iterable,必选,可迭代对象
key,可选,是一个函数,只有一个参数,取自iterable,对iterable进行某种处理。
reverse,可选,排序规则 默认升序 。 降序---reverse=True
作用:按照key的处理结果对iterable进行排序
返回值:返回一个排序后的新列表
>>> l
[1, 2, 5, 4, 3]
>>> sorted(l)
[1, 2, 3, 4, 5]
>>> l
[1, 2, 5, 4, 3]
>>> s="12765"
>>> sorted(s)
['1', '2', '5', '6', '7']
>>> sorted()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Required argument 'iterable' (pos 1) not found
>>>
4、reduce
在 Python3 中,reduce() 函数已经被从全局名字空间里移除了,它现在被放置在 functools 模块里
from functools import reduce
reduce(func,sequence[,initial])
参数:func,函数,有两个参数
sequence,序列
initial,可选,
作用:第一次调用function时,如果提供initial参数,会以sequence中的第一个元素和initial作为参数调用function,
否则会以序列sequence中的前两个元素做参数调用function。第二次以后调用,从sequence中按顺序取第三个
元素,和上一次调用function的结果做参数再次调用function。以此类推,function运算结果再和sequence第四个元素,调用function,....。
返回值:返回对sequence的处理结果
sequence为空时,返回的是initial,若没有initial,则会报错
In [10]: from functools import reduce
...:
...: def add(x,y):
...: return x + y
...:
...: print (reduce(add, range(1, 101)))
5050