Python 四个可以和lambda结合使用的高阶函数

简介: Python 四个可以和lambda结合使用的高阶函数

函数列表

image.png


前三个是内置函数,第四个函数在functools模块中,使用前需要先导入import。

共同点:除sorted外都有一个参数是function,但sorted()的key参数可以是函数。



官方帮助

>>> help(filter)
Help on class filter in module builtins:
class filter(object)
 |  filter(function or None, iterable) --> filter object
 |  
 |  Return an iterator yielding those items of iterable for which function(item)
 |  is true. If function is None, return the items that are true.
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
>>>
>>> help(map)
Help on class map in module builtins:
class map(object)
 |  map(func, *iterables) --> map object
 |  
 |  Make an iterator that computes the function using arguments from
 |  each of the iterables.  Stops when the shortest iterable is exhausted.
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __next__(self, /)
 |      Implement next(self).
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
>>>
>>> help(sorted)
Help on built-in function sorted in module builtins:
sorted(iterable, /, *, key=None, reverse=False)
    Return a new list containing all items from the iterable in ascending order.
    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.
>>>
>>> from functools import reduce
>>> help(reduce)
Help on built-in function reduce in module _functools:
reduce(...)
    reduce(function, sequence[, initial]) -> value
    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.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5).  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.
>>> 



用法举例


一、 filter()

  用于过滤序列,序列中的每个元素作为参数传递给函数进行判断,返回True或者False,最后将返回True的元素放到新列表中。

...举例中

目录
相关文章
|
5月前
|
存储 Python
Python函数式编程,如何编写匿名函数(lambda表达式)?
Python函数式编程,如何编写匿名函数(lambda表达式)?
|
5月前
|
Python
Python解包参数列表及 Lambda 表达式
Python解包参数列表及 Lambda 表达式
|
5月前
|
Python
函数式编程与装饰器:解释什么是高阶函数,并给出几个Python内置的高阶函数示例。编写一个Python装饰器,用于记录函数执行的时间。
函数式编程与装饰器:解释什么是高阶函数,并给出几个Python内置的高阶函数示例。编写一个Python装饰器,用于记录函数执行的时间。
|
2月前
|
程序员 数据处理 开发者
【Python小技巧】通过实例说明推导式,条件表达式和Lambda函数
【Python小技巧】通过实例说明推导式,条件表达式和Lambda函数
23 2
|
2月前
|
Python
请解释Python中的lambda函数是什么?并举例说明其用法。
请解释Python中的lambda函数是什么?并举例说明其用法。
14 2
C4.
|
2月前
|
程序员 数据处理 Python
Python中lambda表达式
Python中lambda表达式
C4.
10 1
|
2月前
|
Python
Python中的Lambda函数应用及性能优化
Lambda函数是Python中一种简洁而强大的编程工具,本文将介绍Lambda函数的基本语法及在实际开发中的应用场景,同时探讨如何通过性能优化提升Lambda函数的执行效率。
|
2月前
|
Python
Python中lambda表达式学习
Python中lambda表达式学习
16 0
|
3月前
|
自然语言处理 安全 Python
Python中的闭包和高阶函数详解
Python中的闭包和高阶函数详解
|
4月前
|
存储 Serverless 数据处理