python lambda拉姆达表达式「建议收藏」
大家好,又见面了,我是你们的朋友全栈君。
>>> lambda x: x + 1
<function <lambda> at 0x000001FC51317840>
看出来,就是一个函数,也是一个表达式。 Because a lambda function is an expression, it can be named. Therefore you could write the previous code as follows:
>>> addone= lambda x:x+1
>>> addone(50)
51
>>> (lambda x:x+1)(50)
51
带2个参数的lambda expression:
>>> sum = lambda x,y :x+y
>>> sum(5,10)
15
>>>
MAP 是什么 ,WTF is MAP? map(function, iterable, …) Return an iterator that applies function to every item of iterable, yielding the results.意思就是把这个函数呢应用到每个iterable 的items上。 看下面的例子
def calculateSquare(n):
return n*n
numbers = (1, 2, 3, 4)
result = map(calculateSquare, numbers)
print(result)
<map object at 0x000001FC51314DD8>
converting map object to set:
numbersSquare = set(result)
print(numbersSquare)
{16, 1, 4, 9} 从结果可见,就是把calculateSquare函数,应用到了每个numbers上。
既然是把函数应用的每个iterable上,自然会想起我们的lambda函数 :
numbers = (1, 2, 3, 4)
result = map(lambda x: x*x, numbers)
print(result)
<map object at 0x00000181A0A280F0>
# converting map object to set
numbersSquare = set(result)
print(numbersSquare)
<map 0x7fafc21ccb00>
{16, 1, 4, 9}
**
Use of lambda() with filter()
**
The filter() function in Python takes in a function and a list as arguments. This offers an elegant way to filter out all the elements of a sequence “sequence”, for which the function returns True. Here is a small program that returns the odd numbers from an input list:
>>> li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
>>> final_list = list(filter(lambda x: (x%2 != 0) , li))
>>> print(final_list)
[5, 7, 97, 77, 23, 73, 61]
>>>
Use of lambda() with reduce()
The reduce() function in Python takes in a function and a list as argument. The function is called with a lambda function and a list and a new reduced result is returned. This performs a repetitive operation over the pairs of the list. This is a part of functools module. Example: 注意,reduce已经被搬到了functools https://docs.python.org/3/library/functools.html#functools.reduce
>>> from functools import reduce
>>> li = [5, 8, 10, 20, 50, 100]
>>> sum = reduce((lambda x, y: x + y), li)
>>>
>>> sum
193
>>>
Here the results of previous two elements are added to the next element and this goes on till the end of the list like (((((5+8)+10)+20)+50)+100).
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/191880.html原文链接:https://javaforall.cn
相关文章
- pycharm配置Python环境_用虚拟机在D盘创建一个虚拟环境
- pycharm如何运行ipynb_python安装jupyter
- Python-drf前戏38-前端Vue
- Python基础08-名称空间与作用域
- 使用 setup.py 将 Python 库打包分发到 PyPI 踩坑指南
- pythoncharm注释快捷键_JAVA注释快捷键
- 用 Python 破解 WiFi 密码,太刺激了!
- Python项目51-课程页面
- pycharm2020.3.4安装教程_python安装pycharm的方法
- Python安装管理 Pycharm安装使用 pip使用[通俗易懂]
- Python基础18-异常处理
- Python项目47-前后端分离登录注册页(继续撸)
- Python暴力激活成功教程密码
- Python进阶41-drf框架(三)
- python+pycharm+pyqt5安装教程「建议收藏」
- Python应用实践——设计一个学生管理系统
- python虚拟环境virtualenv_怎样用pycharm写代码
- linux安装python虚拟环境_windows安装python虚拟环境
- Python基础13-模块的使用
- python Pycharm 更换pip源为国内站点,加速访问