Python中sorted函数、filter类、map类、reduce函数


Python中使用函数作为参数的内置函数和类:

函数名或类名 功能 参数描述
sorted函数 用来将一个无序列表(元组)进行排序 函数参数的返回值规定按照元素的哪个属性进行排序
filter类 用来过滤一个列表里符合规定的所有元素,得到的结果是一个迭代器 函数参数的返回值指定元素满足的过滤条件
map类 将列表里的每一项数据都执行相同的操作,得到的结果是一个迭代器 函数参数用来指定列表里元素所执行的操作
reduce函数 对一个序列进行压缩运算,得到一个值。python3以后,这个方法被移到了functools模块 函数参数用来指定元素按照哪种方式合并

sorted函数

一、sort方法

会直接对列表(元组)进行排序。

nums = [4, 8, 2, 1, 7, 6]
nums.sort()
print(nums)  # [1, 2, 4, 6, 7, 8]

二、sorted内置函数

不会改变原有的数据,而是生成一个新的有序的列表。

ints = [5, 9, 2, 1, 3, 8, 7, 4]
x = sorted(ints)
print(x)  # [1, 2, 3, 4, 5, 7, 8, 9]

三、情景引入

有这样一道题,对下面列表里的数据按照score进行升序排序。

students = [
    {'name': 'zhangsan', 'age': 18, 'score': 92},
    {'name': 'lisi', 'age': 20, 'score': 90},
    {'name': 'wangwu', 'age': 19, 'score': 95},
    {'name': 'jerry', 'age': 21, 'score': 98},
    {'name': 'chris', 'age': 17, 'score': 100},
]

如果直接对students进行排序

students.sort()
print(students)

会报错,错误信息是:TypeError: '<' not supported between instances of 'dict' and 'dict'意思就是说字典和字典之间不能使用比较运算。

sort函数中有一个可选参数keykey参数类型是函数。需要给参数key指定比较规则,最简单的用法如下,先感受一下子。

x = ['mmm', 'mm', 'mm', 'm']
x.sort(key=len)
print(x)  # ['m', 'mm', 'mm', 'mmm']

接下来完成对students列表进行排序

def foo(ele):
    return ele['score']  # 通过返回值告诉sort方法,按照元素的那个属性进行排序

students.sort(key=foo)
print(students)

sort内部实现的时候,调用了foo方法,这个方法需要传入了一个参数,这个参数就是列表里的元素,列表中的元素此时是字典,通过返回值告诉sort方法,按照元素的那个属性进行排序。

上面代码也可以利用lambda表达式简化:

扫描二维码关注公众号,回复: 9951930 查看本文章
students = [
    {'name': 'zhangsan', 'age': 18, 'score': 92},
    {'name': 'lisi', 'age': 20, 'score': 90},
    {'name': 'wangwu', 'age': 19, 'score': 95},
    {'name': 'jerry', 'age': 21, 'score': 98},
    {'name': 'chris', 'age': 17, 'score': 100},
]

students.sort(key=lambda ele: ele['score'])
print(students)

filter类

filter 对可迭代对象进行过滤,得到的是一个filter对象。Python2的时候是内置函数,Python3修改成了一个内置类。

filter可以给定两个参数,第一个参数是函数,第二个参数是可迭代对象。可迭代对象的每个元素作为参数传递给函数进行判断,过滤掉不符合条件的元素,返回一个迭代器对象。filter结果是一个 filter 类型的对象,filter对象也是一个可迭代对象。

一、简单使用

打印列表ages中大于18的元素。

ages = [12, 23, 30, 17, 16, 22, 19]
x = filter(lambda ele: ele > 18, ages)
print(x)  # <filter object at 0x000002670373E908>

# 可迭代对象可以进行遍历
# for a in x:
#     print(a)

adult = list(x)
print(adult)  # [23, 30, 22, 19]

二、练习

有这样一道题,得到下面列表中score大于95的学生。

students = [
    {'name': 'zhangsan', 'age': 18, 'score': 92},
    {'name': 'lisi', 'age': 20, 'score': 90},
    {'name': 'wangwu', 'age': 19, 'score': 95},
    {'name': 'jerry', 'age': 21, 'score': 98},
    {'name': 'chris', 'age': 17, 'score': 100},
]
students = [
    {'name': 'zhangsan', 'age': 18, 'score': 92},
    {'name': 'lisi', 'age': 20, 'score': 90},
    {'name': 'wangwu', 'age': 19, 'score': 95},
    {'name': 'jerry', 'age': 21, 'score': 98},
    {'name': 'chris', 'age': 17, 'score': 100},
]
x = filter(lambda ele: ele['score'] > 95, students)
result = list(x)
print(result)  # [{'name': 'jerry', 'age': 21, 'score': 98}, {'name': 'chris', 'age': 17, 'score': 100}]

map类

将列表里的每一项数据都执行相同的操作,得到的结果是一个迭代器。函数参数用来指定列表里元素所执行的操作。

语法:

map(function, iterable1, iterable2 ...)

一、简单使用

将列表ages中所有元素乘以2。

ages = [12, 23, 30, 17, 16, 22, 19]
m = map(lambda ele: ele * 2, ages)
print(list(m))  # [24, 46, 60, 34, 32, 44, 38]

二、练习

有这样一道题,为students中每个学生添加一个sex属性,都为男性。

students = [
    {'name': 'zhangsan', 'age': 18, 'score': 92},
    {'name': 'lisi', 'age': 20, 'score': 90},
    {'name': 'wangwu', 'age': 19, 'score': 95},
    {'name': 'jerry', 'age': 21, 'score': 98},
    {'name': 'chris', 'age': 17, 'score': 100},
]
students = [
    {'name': 'zhangsan', 'age': 18, 'score': 92},
    {'name': 'lisi', 'age': 20, 'score': 90},
    {'name': 'wangwu', 'age': 19, 'score': 95},
    {'name': 'jerry', 'age': 21, 'score': 98},
    {'name': 'chris', 'age': 17, 'score': 100},
]
x = map(lambda ele: ele.setdefault('sex', 'male'), students)
result = list(x)
print(students)

reduce函数

对一个序列进行压缩运算,得到一个值。python2中,reduce是一个内置函数。python3以后,这个方法被移到了functools模块。函数参数用来指定元素按照哪种方式合并。

语法:

reduce(function, iterable[, initializer])

  • function —> 函数,有两个参数
  • iterable —> 可迭代对象
  • initializer —> 可选,初始值

函数将一个iterable中的所有数据进行下列操作:函数 function(有两个参数)先对iterable中的第 1、2 个元素进行操作,得到的结果再与第三个元素用 function 函数运算,最后得到一个结果。

一、简单使用

from functools import reduce  # 导入模块的语法


def foo(x, y):  # x=2,y=1;x=3,y=6;x=9,y=8;最后返回x+y为17
    return x + y 


nums = [2, 1, 6, 8]
print(reduce(foo, nums))  # 17

二、设置初始值

无初始值时从序列的第一个参数开始。

from functools import reduce  # 导入模块的语法


def foo(x, y):  # x=10,y=2;x=12,y=1;x=13,y=6;x=19,y=8最后返回x+y为27
    return x + y


nums = [2, 1, 6, 8]
print(reduce(foo, nums, 10))  # 27

将上述代码优化后,如下:

from functools import reduce  # 导入模块的语法
nums = [2, 1, 6, 8]
print(reduce(lambda x, y: x + y, nums, 10))  # 27
发布了31 篇原创文章 · 获赞 48 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/mall_lucy/article/details/104591836