python中高阶函数及内置高阶函数(map、reduce、filter、sorted)

1.什么是高阶函数
变量可以指向函数,函数的参数能够接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数
所谓高阶函数,就是函数中可以传入另一个函数作为参数的函数
函数赋值变量

1def fun(a,b):
    return a+b
a = fun(1,3)  
print(a)
2:
f = abs    变量赋值函数,不用括号
print(f(-10))
打印结果
10

abs()函数求绝对值

print(abs(-10))
print(abs(10))
打印结果
10
10

高阶函数传递的参数包括函数名

def fun(x,y,f):   f定位一个函数将函数当做参数使用
    return f(x),f(y)
print(fun(-10,10,abs))
打印结果
(10, 10)

2.常见的内置高阶函数
map函数
map()函数接收两个参数,一个是函数,一个是序列
map将传入的函数依次作用到序列的每个元素,并且把结果
作为新的序列返回

对于序列[-1,3,-5,7,-9]的每个元素求绝对值
print(list(map(abs,[-1,3,-5,7,-9])))
打印结果
[1, 3, 5, 7, 9]
102-7之间的随机数,对每个元素求阶乘
import random
def f(x):
    res = 1
    for i in range(1,x+1):
        res *= i
    return res
li = [random.randint(2,7) for i in range(10)]
print(li)
print(list(map(f,li)))
打印结果
[3, 7, 2, 2, 2, 4, 7, 2, 3, 2]
[6, 5040, 2, 2, 2, 24, 5040, 2, 6, 2]

reduce()函数
reduce:把一个函数作用在一个序列上,这个函数必须接收两个
参数,reduce把结果继续和序列的下一个元素做累积计算
reduce(f,[x1,x2,x3,x4,x5]) = f(f(f(x1,x2),x3),x4)

python2:reduce是内置函数
python3:from functools import reduce
from functools import  reduce
def mulit(x,y):
    return x*y    使用reduce累乘算法
print(reduce(mulit,range(1,5)))
打印结果
24
from functools import  reduce
def add(x,y):
    return x+y   使用reduce累加
print(reduce(add,[1,2,3,4,5]))
打印结果
15

filter过滤函数
和map()类似,也接收一个函数和一个序列
但是和map()不同的是,filter()把传入的函数依次作用于
每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素

列出100以内的偶数
def isodd(num):
    if  num % 2 == 0:
        return True
    else:
        return False
print(list(filter(isodd,range(100)
打印结果
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36,
 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 
 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
 列出100以内的质数
 def isodd(num):
    for i in  range(2,101):
        if  num % i == 0:
            return False
        else:
            return True
 print(list(filter(isodd,range(2,101))))
 打印结果
 [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 
 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 
 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 
 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]

int也是函数

sorted函数
默认sort方法和sorted函数均由小到大排序,reverse=True由大到小排序

默认从小到大
sort函数
li = [1,2,3,4,5,6,7,]
li.sort()
print(li)
打印结果
[1, 2, 3, 4, 5, 6, 7]
sorted()函数
li = [5,6,2,1,3]
li = sorted(li)
print(li)
[1, 2, 3, 5, 6]

reverse=True由大到小排序
sort函数
li = [1,2,3,4,5,6,7,]
li.sort(reverse=True)
print(li)
打印结果
[7, 6, 5, 4, 3, 2, 1]
sorted函数
li = [5,6,2,1,3]
li = sorted(li,reverse=True)
print(li)
打印结果
[6, 5, 3, 2, 1]

int也是函数
用户接收一串数字,‘1 3 4 5 6’,将字符串中所有的数字转换为整型,并以列表的格式输出

s = '1 2 3 4 5'
print(list(map(int,s.split())))

匿名函数
高阶函数应用

info = [
    # 商品名称 商品数量 商品价格
    ('apple1',200,32),
    ('apple2',40,12),
    ('apple3',40,2),
    ('apple4',1000,23),
    ('apple5',40,5)
]
以上商品排序
print(info)默认排序名称大小排序
def sorted_by_conut(x):    
    return x[1]
print(sorted(info,key=sorted_by_conut))  按照数量排序从小到大
def sorted_by_price(x):
    return x[2]
print(sorted(info,key=sorted_by_price))   按照价格排序
def sorted_by_count_price(x):
    return x[1],x[2]
print(sorted(info,key=sorted_by_count_price))  按照数量价格一起排序
打印结果
[('apple1', 200, 32), ('apple2', 40, 12), ('apple3', 40, 2), ('apple4', 1000, 23), ('apple5', 40, 5)]
[('apple2', 40, 12), ('apple3', 40, 2), ('apple5', 40, 5), ('apple1', 200, 32), ('apple4', 1000, 23)]
[('apple3', 40, 2), ('apple5', 40, 5), ('apple2', 40, 12), ('apple4', 1000, 23), ('apple1', 200, 32)]
[('apple3', 40, 2), ('apple5', 40, 5), ('apple2', 40, 12), ('apple1', 200, 32), ('apple4', 1000, 23)]

给定一个整形数组, 将数组中所有的0移动到末尾, 非0项保持不变;
在原始数组上进行移动操作, 勿创建新的数组;
输入:
第一行是数组长度, 后续每一行是数组的一条记录;
4
0
7
0
2
输出:
调整后数组的内容;
4
7
2
0
0

n = ''.join(input().split())  输入字符分割并连接 
li = [int(i) for i in n]    循环遍历输入的数值,把字符串转换为整型,
def move_zero(item):      定义函数
    if item == 0:      
        return  2
    else:
        return 1
print(sorted(li,key=move_zero))
打印结果
40702
[4, 7, 2, 0, 0]
解释函数返回值return=1/2
4   不等于0  返回值为1
0    等于0    返回值为0
7               1
2				1
0				0	
0				0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值