内置函数

  1 常用:
  2 
  3 type()
  4 
  5 int()
  6 
  7 float()
  8 
  9 list()
 10 
 11 str()
 12 
 13 tuple()
 14 
 15 set()
 16 
 17 dict()
 18 
 19  
 20 
 21 sum() #计算总和
 22 
 23 L = [1,2,3,5,23,235,2]
 24 
 25 avg = sum(L) / len(L)
 26 
 27 print(avg)
 28 
 29 max() #最大值
 30 
 31 print(max(L))
 32 
 33 min() #最小值
 34 
 35 print(min(L))
 36 
 37 round() #保留小数点几位数
 38 
 39 print(round(avg,2))
 40 
 41 print(divmod(10,3)) #取商和余数的
 42 
 43 zip() #解包
 44 
 45 us = ['admin','test','dev']
 46 
 47 ps = [123,456,789]
 48 
 49 for u,p in zip(us,ps):
 50 
 51   print(u,p)
 52 
 53 print(list(zip(us,ps)))
 54 
 55 print(tuple(zip(us,ps)))
 56 
 57 s = '55132145120545'
 58 
 59 print(''.join(sorted(s)) #排序
 60 
 61 d = {'admin':89,'test':100,'dev':61} #给字典排序
 62 
 63 def x(L):
 64 
 65   print(L)
 66 
 67   return L[1]
 68 
 69  print(sorted(d.items(),key=x,reverse=True)))  #key 指定的是函数名 sorted会自动循环
 70 
 71 lambda #匿名函数
 72 
 73 print(sorted(d.itmes(),key=lambda x:x[1],reverse = True))
 74 
 75 lambda x:x+1 #匿名函数 只能写简单函数 x是入参,x+1 是返回值
 76 
 77 def add(x):
 78 
 79   return x+1
 80 
 81 L = [1,2,3,4,43,634,63,4634,63,636,23]  #转成字符串
 82 
 83 ls = []
 84 
 85 for i in L:
 86 
 87   i = str(i)
 88 
 89   ls.append(i)
 90 
 91 print(ls)
 92 
 93  
 94 
 95 def t(x):
 96 
 97   x = str(x)#强转为str
 98 
 99   if len(x) == 1:
100 
101     return "00" + x
102 
103   elif len(x) == 2:
104 
105     return "0" +x
106 
107   print("t',x)
108 
109   return x
110 
111 L2 = list(map(lambda x:str(x),L))
112 
113 L2 = list(map(t,L))  #循环调用指定的函数,保存结果到一个list里面
114 
115 L3 = list(map(str,l))  #str() 也是个函数
116 
117 print(L2)
118 
119 print(L3)
120 
121  
122 
123 filter() #过滤
124 
125 L = [1,2,3,4,43,634,63,4634,63,636,23] 
126 
127 def func(x):
128 
129   return x % 2 == 0
130 
131 print(list(filter(func,L))
132 
133 print(list(map(func,L))

 

posted @ 2021-05-21 09:51  等待iing  阅读(69)  评论(0)    收藏  举报