python高阶函数map reduce filter sorted_python高阶函数map & filter & reduce & sorted

高阶函数为把函数作为参数传入的函数

引用链接

高阶函数

环境

python3.6

map

map函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到每个序列对应索引的元素,最后结果作为生成器返回。序列参数可传入多个序列

1. 多个序列时,若是长度最短的序列所有元素都已映射,则整个map函数结束

from collections import Iterator

a_li, b_li, c_li = list(range(10)), list(range(104)), list(range(1, 15))

m1 = map(lambda x, y, z: x + y + z, a_li, b_li, c_li)

print(isinstance(m1, Iterator))

print(list(m1))

返回的生成器只迭代到最短的序列的所有元素,结果为

True

[1, 4, 7, 10, 13, 16, 19, 22, 25, 28]

2. 函数参数除了使用匿名函数,也可以使用自定义的映射函数

def map_func(x, y, z):

return x + y + z + 10

m2 = map(map_func, a_li, b_li, c_li)

print(list(m2))

传入函数名,结果为

[11, 14, 17, 20, 23, 26, 29, 32, 35, 38]

reduce

reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是

reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

1. 将数字字符串转为整数

s = "12344"

num_dic = {num: int(num) for num in "0123456789"}

res = reduce(lambda x, y: x * 10 + y, map(lambda i: num_dic[i], s))

print(res)

map将字符串转为数字序列并返回生成器,redcue将序列根据匿名函数转为整数,结果为

12344

reduce中initial参数为初始化时匿名函数的第一个参数,initial=9

res2 = reduce(lambda x, y: x * 10 + y, map(lambda i: num_dic[i], s), 9)

print(res2)

结果为

912344

filter

filter()函数用于过滤序列。filter()接收一个函数和一个序列,把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素,最后返回生成器对象

1. 过滤 b_li 序列,返回平方根为整数的生成器

import math

f2 = filter(lambda x: math.sqrt(x) % 1 == 0, list(range(100)))

print(list(f2))

对平方根的值与1作除法获取余数,结果为

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

sorted

排序是比较两个元素的大小。数字直接比较,字符串根据ascii比较,dict可获取items()根据健、值等元素比较。key参数可以实现自定义的排序,即排序关键在于实现一个映射函数

1. 实现数字的降序排序,reversse参数默认为False升序。

order_li = sorted([36, 5, -12, 9, -21], reverse=True)

print(order_li)

结果为

[36, 9, 5, -12, -21]

2. 实现数字绝对值的升序排序

order_li = sorted([36, 5, -12, 9, -21], key=lambda x: abs(x))

print(order_li)

结果为

[5, 9, -12, -21, 36]

3. 忽略大小写,实现首字母的升序排序

order_li = sorted([‘bob‘, ‘Zoo‘, ‘Credit‘], key=lambda x: x.lower())

print(order_li)

转为全部小写,根据首字母排序,结果为

[‘bob‘, ‘Credit‘, ‘Zoo‘]

4. 字典排序。根据健升序排序

dic = {2: [30, ‘y‘], 1: [96, ‘i‘], 3: [21, ‘w‘], 9: [40, ‘y‘], 7: [20, ‘y‘]}

print(dic.items())

order_dic_li = sorted(dic.items(), key=lambda x: x[0])

order_dic = {i[0]: i[1] for i in order_dic_li}

print(order_dic)

字典获取items()方法,对象为dict_items。根据健排序即根据dict_items对象的第一个元素排序,结果为

dict_items([(2, [30, ‘y‘]), (1, [96, ‘i‘]), (3, [21, ‘w‘]), (9, [40, ‘y‘]), (7, [20, ‘y‘])])

{1: [96, ‘i‘], 2: [30, ‘y‘], 3: [21, ‘w‘], 7: [20, ‘y‘], 9: [40, ‘y‘]}

5. 字典排序。根据值的元素排序,优先级为值的第二个元素、第一个元素,两者为升序排序

order_dic_li = sorted(dic.items(), key=lambda x: (x[1][1], x[1][0]))

order_dic = {i[0]: i[1] for i in order_dic_li}

print(order_dic)

根据值的两个元素排序,结果为

{1: [96, ‘i‘], 3: [21, ‘w‘], 7: [20, ‘y‘], 2: [30, ‘y‘], 9: [40, ‘y‘]}

6. 根据字典值的元素排序,优先级为值的第二个元素、第一个元素;排序为第二个元素升序,第一个元素降序

order_dic_li = sorted(dic.items(), key=lambda x: (x[1][1], -x[1][0]))

order_dic = {i[0]: i[1] for i in order_dic_li}

print(order_dic)

结果为

{1: [96, ‘i‘], 3: [21, ‘w‘], 9: [40, ‘y‘], 2: [30, ‘y‘], 7: [20, ‘y‘]}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值