ToB企服应用市场:ToB评测及商务社交产业平台

标题: Python中常用的几个内置方法(max()/min()、filter()、map()、sorted、redu [打印本页]

作者: 用户国营    时间: 2024-6-13 18:02
标题: Python中常用的几个内置方法(max()/min()、filter()、map()、sorted、redu
1.max()/min()

  1. """
  2.     max(iterable, *[, default=obj, key=func]) -> value
  3.     max(arg1, arg2, *args, *[, key=func]) -> value
  4.    
  5.     With a single iterable argument, return its biggest item. The default keyword-only argument specifies an object to return if the provided iterable is empty.
  6.     With two or more arguments, return the largest argument.
  7. """
  8. res = max([1, 2, 3], default=0)
  9. print(res)  # 3
  10. # 传入了一个空的可迭代的对象参数, 返回默认值0
  11. res = max([], default=0)
  12. print(res)  # 0
  13. lis = [1, 2, 3]
  14. def func(num):
  15.     return -num
  16. # res = max(lis, key=func)
  17. res = max(lis, key=lambda num: -num)
  18. print(res)  # 1
复制代码
key参数接收的是一个函数对象
max函数会将lis里面的元素依次传入转换函数
哪个元素经过转换函数得到的值最大, 就返回该元素
2.filter() 过滤

第一个参数(形参), 要么是func, 要么是None, 不传会报错
第二个参数是可迭代对象
返回一个filter obj (iterator)
filter()方法会过滤掉:
  1. filter(function or None, iterable) --> filter object
  2. Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.
复制代码
需要传入两个参数, 第一个是函数或者None, 第二个是可迭代对象
返回的是一个filter obj(迭代器)
如果第一个参数时None, 则返回的迭代器中只包罗可迭代对象中为True的元素
如果第一参数是一个函数, 可迭代对象中元素传入该函数, 结果为True, 则filter方法返回的迭代器就会
包罗此元素
  1. lis = [0, 1, 2, 3]
  2. filter_obj = filter(None, lis)
  3. print(list(filter_obj))  # [1, 2, 3]
  4. def func(num):
  5.     if num > 1:
  6.         return 0
  7. filter_obj = filter(func, lis)
  8. print(list(filter_obj))  # []
  9. filter_obj = filter(lambda num: num > 1, lis)
  10. print(list(filter_obj))  # [2, 3]
复制代码
3.map() 映射

第一个参数必须是函数
后面可传入一个或多个可迭代对象参数
  1. """
  2.     map(func, *iterables) --> map object
  3.    
  4.     Make an iterator that computes the function using arguments from
  5.     each of the iterables.  Stops when the shortest iterable is exhausted.   
  6. """
  7.    
  8. def func1(x):
  9.     return x + 1
  10. """
  11. 参数1: 函数, 参数2:可迭代对象
  12. 1.可迭代对象的中的元素依次传入函数得到返回值res
  13. 2.调用map函数最终会得到一个迭代器对象iterator
  14. 3. 这个iterator就包含了res
  15. """
  16. map_obj = map(func1, [1, 2, 3])
  17. print(list(map_obj))  # [2, 3, 4]
  18. def func2(x, y):
  19.     return x + y
  20. """
  21. 传入的可迭代对象参数个数与函数所需要的参数个数是相等的
  22. 取值个数以最短的为准
  23. """
  24. map_obj = map(func2, [1, 2, 3], [1, 2, 3, 4])
  25. print(list(map_obj))  # [2, 4, 6]
复制代码
4.sorted筛选

  1. """
  2.         sorted(iterable, key, reverse) --> list
  3.        
  4.     Return a new list containing all items from the iterable in ascending order.
  5.     A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
  6. """
  7. lis = [3, 2, 4, 5, 1]
  8. # 1.只传入可迭代对象参数
  9. res = sorted(lis)
  10. print(res)  # [1, 2, 3, 4, 5]
  11. def func(x):
  12.     return -x
  13. #2.当传入函数时, 可迭代对象元素排序的依据是他们传入函数得到结果
  14. #注意: 还是对原来的元素进行排序, 而不是对元素传入函数得到的结果, 只是以这个结果为排序的依据
  15. res = sorted(lis, key=func)
  16. print(res)  # [5, 4, 3, 2, 1]
复制代码
5.reduce()淘汰

  1. from functools import reduce
  2. """
  3.     reduce(function, sequence[, initial]) -> value
  4.     Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value.
  5.     For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5).  
  6.     If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.
  7. """
  8. #学习中遇到问题没人解答?小编创建了一个Python学习交流群:153708845
  9. lis = [1, 2, 3, 4, 5]
  10. res1 = reduce(lambda x, y: x + y, lis)
  11. print(res1)  # 15
  12. res2 = reduce(lambda x, y: x + y, lis, 1)
  13. print(res2)  # 16
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4