python(day16)内置函数,匿名函数
发布时间:2020-12-17 00:01:55 所属栏目:Python 来源:网络整理
导读:div class="cnblogs_code" span style="color: #008000"# span style="color: #008000" dic={'k1':10,'k2':100,'k3':30} span style="color: #008000" span style="color: #008000" def func(key): span style="color: #008000" span style="color: #008000"
<div class="cnblogs_code">
匿名函数基本格式:? ?func= lambda i : ret? ? ? ? # i 是形参,ret 是返回值? ?func() #调用匿名函数
# 匿名函数 == 内置函数
# zip 拉链排序方法,内置函数之一
# ret = zip((('a'),('d')))
# res = map(lambda tup:{tup[0]:tup[1]},ret)
# print(list(res))
def multipliers():return [lambda x:i*x for i in range(4)]print([m(2) for m in multipliers()])
内置函数:1.reverse(注意,都是返回的贴带起,如果想看内容,就要用for方法)2.slice,format<span style="color: #008000"># <span style="color: #008000"> print(format('test','<20'))<span style="color: #008000"> <span style="color: #008000"> print(format('test','>40'))<span style="color: #008000"> <span style="color: #008000"> print(format('test','^40'))3.bytes,bytearray<span style="color: #008000"># <span style="color: #008000"> 网络编程 只能传二进制<span style="color: #008000"> <span style="color: #008000"> 照片和视频也是以二进制存储<span style="color: #008000"> <span style="color: #008000"> html网页爬取到的也是编码<span style="color: #008000"> <span style="color: #008000"> b_array = bytearray('你好',encoding='utf-8')<span style="color: #008000"><span style="color: #008000"> print(b_array)<span style="color: #008000"><span style="color: #008000"> print(b_array[0])<span style="color: #008000"><span style="color: #008000"> 'xe4xbdxa0xe5xa5xbd'<span style="color: #008000"><span style="color: #008000"> s1 = 'alexa'<span style="color: #008000"><span style="color: #008000"> s2 = 'alexb'# 切片 —— 字节类型 不占内存# 字节 —— 字符串 占内存4.ord,unicode转换为数字表示,chr,数字转换为unicode表示5.repr,完全复制6.all,有一个false,则返回false,? ? ? ? ? ? ? ? ?any有一个true,则返回true<span style="color: #008000"># <span style="color: #008000"> print(any(['',True,[]]))7.5个超重要函数,zip(拉链),filter(筛选),map(遍历)?,sort (在原列表排序),sorted(生成新列表),?<span style="color: #008000"># <span style="color: #008000"> ret = map(abs,-4,6,-8])<span style="color: #008000"> <span style="color: #008000"> print(ret)<span style="color: #008000"> <span style="color: #008000"> for i in ret:<span style="color: #008000"> <span style="color: #008000"> print(i)<span style="color: #008000"># <span style="color: #008000"> filter 执行了filter之后的结果集合 <= 执行之前的个数<span style="color: #008000">#<span style="color: #008000">filter只管筛选,不会改变原来的值<span style="color: #008000"> <span style="color: #008000"> map 执行前后元素个数不变
<span style="color: #008000">#<span style="color: #008000"> l = [1,-10]<span style="color: #008000"> <span style="color: #008000"> # l.sort(key = abs) # 在原列表的基础上进行排序<span style="color: #008000"><span style="color: #008000"> # print(l)<span style="color: #008000"><span style="color: #008000"> print(sorted(l,key=abs,reverse=True)) # 生成了一个新列表 不改变原列表 占内存<span style="color: #008000"><span style="color: #008000"> print(l)<span style="color: #008000">#<span style="color: #008000"> l = [' ',2],'hello world']<span style="color: #008000"> <span style="color: #008000"> new_l = sorted(l,key=len)<span style="color: #008000"><span style="color: #008000"> print(new_l)(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |