内置函数配合匿名函数使用
1.map()映射
'''# map也像for循环一样把每一个数据值映射出来交给下面的函数名'''
已知现在有一个列表,需要给列表中每个元素开方
ll =[1,2,3,4,5,6]'''正常方式'''# 定义一个空列表
num_list =[]for i in ll:
num_list.append(i **2)print(num_list)'''函数'''defindex(x):return x **2
res =map(index, ll)print(res)# <map object at 0x0000024D961AC3D0>print(list(res))# 需要转换成对应数据类型'''匿名函数'''
res =map(lambda x: x **2, ll)print(list(res))# 需要转换成对应的数据类型'''
底层也是for循环 循环出每一个数据值,把数据值交给函数去处理,
就会返回数据值,依次顺序循环 这就是映射
'''
2.zip()拉链
2.zip()拉链
#已知现在有两个列表我们需要给他们依次排列
name_list =['name','age','gender']
zhi_list =['chen',18,'male']'''正常方式'''# 定义一个空列表
new_list =[]for i inrange(len(name_list)):
new_list.append((name_list[i],zhi_list[i]))print(new_list)'''匿名函数'''
res =zip(name_list,zhi_list)print(res)# <zip object at 0x000001EBC57DA980>print(list(res))'''
注意zip只会连接相同的两个列表 如果列表数据值有多有少的,
那么只会对应着连接最少数据值的,多的列表的多的数据值会被忽略
并且可以多个列表来连接,但是还是一样只能连接多个列表中数据值最少的个数值部分
'''
3.max()最大值 min()最小值
3.max()最大值 min()最小值
max/min 是不允许跨数据类型比较
# 返回列表中最大值和最小值
ll =[1,2,3,4,45,5,63,34,234,232,23]'''正常方式'''# 通过排序来直接获取最大值/最小值
ll.sort()# 默认从小到大顺序print(ll)print(ll[-1])
ll.sort(reverse=True)#倒序print(ll)print(ll[-1])'''max、min方式'''
res =max(ll)print(res)
res1 =min(ll)print(res1)'''
注意:
max、min取字典的时候是默认取的是key值
也是按照key值来比较的
'''
new_dict ={'name':'chen','age':"18",'gender':'male'}
res =max(new_dict)print(res)# name 返回的是key值,并且是按照ASCII编码来比较的
res1 =min(new_dict)print(res1)# age'''函数'''defindex(x):return new_dict[x]
res =max(new_dict,key=index)# 这样就是取的value值print(res)
res =min(new_dict,key=index)# 这样就是取的value值print(res)'''匿名函数'''
res =max(new_dict, key=lambda x:new_dict[x])print(res)
res =min(new_dict, key=lambda x:new_dict[x])print(res)
4.filter()过滤
4.filter()过滤
#列表中里面有很多数据值,我们需要移除44
ll =[11,22,33,44,55,66,77]'''正常方式'''
new_list =[]for i in ll:if i ==44:passelse:
new_list.append(i)print(new_list)'''函数'''defindex(x):return x !=44
res =filter(index, ll)# 内部也是for循环print(list(res))'''匿名函数'''
res =filter(lambda x : x!=44, ll)# 内部也是for循环print(list(res))
常用内置函数
# from functools import reduce'''传多个值,返回一个值'''# ll = [11,22,33,44,55,66]# res = reduce(lambda a,b:a*b,ll)# print(res)'''
1.导入 reduce
2.获取列表
3.获得a = 11,b=22 并相乘得242,在赋值给a
4.a=242,或者b=33,a,b相乘得7986再赋值给a
依次循环,直至结束
''''''常用内置函数''''''1.abs()绝对值'''# print(abs(-123)) # 123 # 绝对值就是数学上面的正数也是正数,负数还是正数# print(abs(123)) # 123'''2.all()全 与所有数据对应的布尔值为True结果为True否则为False'''# print(all([0,1,2,3,4,5,66])) # False 0默认是False# print(all([1,2,3,4,True])) # True'''3.any()全 或所有数据对应的布尔值,有一个为True结果为True,否则为False'''# print(any([0,{},[],1,False]))# print(any([0,'',None,False]))'''4.bin() oct() henx() bool()'''# print(bin(100)) # 0b1100100 十进制转二进制# print(oct(100)) # 0o144 十进制转八进制# print(hex(100)) # 0x64 十进制转十六进制'''5.bytes()编码'''# s1 = '人生大写的失败'# print(s1.encode('utf-8')) # b'\xe4\xba\xba\xe7\x94\x9f\xe5\xa4\xa7\xe5\x86\x99\xe7\x9a\x84\xe5\xa4\xb1\xe8\xb4\xa5'# print(bytes(s1,'utf-8'))'''6.callable()判断一个名字是否可以加括号调用'''# name = 'chen'# def index():# print('from index')## print(callable(name)) # False# print(callable(index)) # True'''7.chr() ord() 基于ASICC码表做数字与字母之间的转换'''# print(chr(65)) # A# print(ord('A')) # 65'''8.dir() 查看对象内置方法 返回括号内对象能够调用的名字'''# print(dir('hello wolrd'))'''9.divmod() 整除和取余 返回是一个元组 第一个数据为商,第二个数据是余数'''# res = divmod(100,2)# print(res) # (50,0) 因为是100所以整除了,故余数为0## res1 = divmod(99,2)# print(res1) # (49,1) 99除2得整数49,余1## '''# 总数 每页展示得数据 总页码# 100 10 10# 99 10 10# 102 10 11# '''# page_num,more = divmod(9999,20)# print(divmod(9999,20))# if more:# page_num += 1## print('总页码为:',page_num) # 500'''10.enumerate()枚举'''# ll = 'hello world'# for i,j in enumerate(ll):# print((i,j),end=' ')# #(0, 'h') (1, 'e') (2, 'l') (3, 'l') (4, 'o') (5, ' ') (6, 'w') (7, 'o') (8, 'r') (9, 'l') (10, 'd')### for a,b in enumerate(ll,start = 100):# print(a,b,end=' ')### test = {i:j for i,j in enumerate('happy')}# print(test) # {0: 'h', 1: 'a', 2: 'p', 3: 'p', 4: 'y'}'''11.hash()哈希加密'''# print(hash('hello '))'''
第一次加密6584512372236034569
第二次加密7429857790942834047
每一次加密都不一样,都是随机数
''''''12.id(),input(),isinstance()'''#1.id
ll =23print(id(ll))# 140708403087712 内存地址#2.input'''
input函数作用:接收来自用户得输入
input返回值类型:输入值的类型为str
值的存储:使用=对输入的值进行存储
'''#3.isinstance()# ls = 22# res = isinstance(ll,int)# print(res) # True'''13.open()'''# open(file,mode='r',buffering=None,encoding=None,errors=None,newline=None,closefd=True)# open() 是python的内置函数,可以打开文件对其进行的操作'''
Character Meaning
'r' open for reading (default) 打开可读文件
'w' open for writing, truncating the file first da
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
''''''14.pow()幂指数'''# print(pow(2,4)) # 16# print(pow(2,9)) # 512'''15.range() xrange()'''# print(range(10,)) # range(0, 10)'''16.round() 四舍五入取整'''# print(round(67.3)) # 67# print(round(45.8)) # 46'''17.sum()'''# print(sum([12,34,55,77,89])) # 267'''18.eval()执行字符串类型的代码,并返回结果 exec()执行字符串类型的代码'''# eval('python代码字符版')# exec('代码字符版',{},变量)#执行结束,执行代码,产生属性放到字典里,存到变量中#这个变量存的就是这个代码的名称空间(字典类型)'''19.help()查看函数或模块用途的说明'''# print(help(str)) # 查看字符串的用途