Python 内置函数补充匿名函数
Python3 匿名函数
定义一个函数与变量的定义非常相似,对于有名函数,必须通过变量名访问
def func(x,y,z=1):
return x+y+z
print(func(1,2,3))
匿名函数定义:使用 lambda 来创建匿名函数
匿名函数1. 没有名字 2:函数体自带return
匿名函数
def func(x,y,z=1):
return x+y+z
print(lambda x,y,z=1:x+y+z)
function <lambda> at 0x0000000003469488>
匿名函数也是一个函数对象,匿名函数赋值给一个变量,再利用变量来调用该函数
f=lambda x,y,z=1:x+y+z
print(f)
print(f(1,2,3))
匿名函数的应用场景:应用于一次性的场景,临时使用
2、匿名函数结合使用内置函数的用法
max,min,sorted,map,reduce,filter的用法
字典的运算:最小值,最大值,排序
salaries={
'egon':3000,
'alex':100000000,
'wupeiqi':10000,
'yuanhao':2000
}
print(max(salaries)) # 默认比较key值大小
print(max(salaries.values())) # 比较values值
print(max(salaries,key=lambda name:salaries[name])) 取values,来比较
print(min(salaries,key=lambda name:salaries[name])) 取values,来比较
print(max(zip(salaries.values(),salaries.keys()))[1]) # 通过zip的方式实现
def get_value(name):
return salaries[name]
print(max(salaries,key=get_value))
names=['alex','wupeiqi','yuanhao','yanglei','egon']
res=map(lambda x:x if x == 'egon' else x+'二狗子',names)
print(res)
print(list(res))
names=['alex','wupeiqi','yuanhao','yanglei','egon']
def my_map(func,seq):
for item in seq:
yield func(item)
res1=my_map(lambda x:x+'_二狗',names)
print(next(res1))
print(next(res1))
functools中导入reduce模块
from functools import reduce
print(reduce(lambda x,y:x+y,range(101),100))
print(reduce(lambda x,y:x+y,range(101)))
names=['alex_二狗','wupeiqi_二狗','yuanhao_二狗','yanglei_二狗','egon']
print(list(filter(lambda name:name.endswith('二狗'),names)))
Python 内置函数补充匿名函数的更多相关文章
- Python内置的字符串处理函数整理
Python内置的字符串处理函数整理 作者: 字体:[增加 减小] 类型:转载 时间:2013-01-29我要评论 Python内置的字符串处理函数整理,收集常用的Python 内置的各种字符串处理 ...
- python内置常用高阶函数(列出了5个常用的)
原文使用的是python2,现修改为python3,全部都实际输出过,可以运行. 引用自:http://www.cnblogs.com/duyaya/p/8562898.html https://bl ...
- Python3 内置函数补充匿名函数
Python3 匿名函数 定义一个函数与变量的定义非常相似,对于有名函数,必须通过变量名访问 def func(x,y,z=1): return x+y+z print(func(1,2,3)) 匿名 ...
- Python 内置的一些高效率函数用法
1. filter(function,sequence) 将sequence中的每个元素,依次传进function函数(可以自定义,返回的结果是True或者False)筛选,返回符合条件的元素,重组 ...
- 学习Pytbon第十天 函数2 内置方法和匿名函数
print( all([1,-5,3]) )#如果可迭代对象里所有元素都为真则返回真.0不为真print( any([1,2]) )#如果数据里面任意一个数据为真返回则为真a= ascii([1,2, ...
- python内置方法补充any
any(iterable) 版本:该函数适用于2.5以上版本,兼容python3版本. 说明:如果iterable的任何元素不为0.''.False,all(iterable)返回True.如果ite ...
- Python内置进制转换函数(实现16进制和ASCII转换)
在进行wireshark抓包时你会发现底端窗口报文内容左边是十六进制数字,右边是每两个十六进制转换的ASCII字符,这里使用Python代码实现一个十六进制和ASCII的转换方法. hex() 转换一 ...
- Python内置的字符串处理函数
生成字符串变量 str='python String function' 字符串长度获取:len(str) 例:print '%s length=%d' % (str,len(str)) 连接字符 ...
- python内置方法补充bin
bin(x) 英文说明:Convert an integer number to a binary string. The result is a valid Python expression. I ...
随机推荐
- Swoole编译安装步骤
Swoole扩展是按照php标准扩展构建的.使用phpize来生成php编译配置,./configure来做编译配置检测,make进行编译,make install进行安装. 请下载releases版 ...
- leetcode-字符串篇
Implement strStr() /** * Implement strStr(). * * Return the index of the first occurrence of needle ...
- Linux查看CPU和内存情况
本文简单介绍在Linux上查看CPU和内存情况和一款系统资源查看工具htop. 查看CPU情况 以下是个人工作会经常使用到的服务器的信息. 查看所有CPU信息 可以通过如下命令查看所有CPU信息: # ...
- 【朝花夕拾】Android自定义View篇之(二)Canvas常用功能
前言 转在请申明,转自[https://www.cnblogs.com/andy-songwei/p/10960012.html],谢谢! 上一篇讲View的绘制流程中讲到过,最后一步是draw流程, ...
- Linux系统文件属性知识
---------------------------------------------------------------------------------------------------- ...
- C# webclient progresschanged downlodfileCompleted
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类
因为Jdk7及以前的日期时间类的不方便使用问题和线程安全问题等问题,2005年,Stephen Colebourne创建了Joda-Time库,作为替代的日期和时间API.Stephen向JCP提交了 ...
- 关于async function(){ let res = await } 详解
本文引自: https://www.jianshu.com/p/435a8b8cc7d3 async function fn(){ //表示异步,这个函数里面有异步任务 let result = aw ...
- Xcode报错:could not attach to pid:"1764"
这种错误不是什么问题,按照参考链接操作即可,亲测有效: https://www.cnblogs.com/luorende/p/6295945.html 在运行项目时出现了如下错误 (基本上重新启动项目 ...
- OpenCV:图像平滑和图像模糊处理
导包: import numpy as np import cv2 import matplotlib.pyplot as plt def show(image): plt.imshow(image) ...