赞
踩
匿名函数、生成器、内置函数、推导式
匿名函数lambda
lambda 参数:函数体
def fun2(a1,a2):
return a1 + a2 + 10
# fun2与匿名函数等价
fun1 = lambda a1,a2: a1 + a2 + 10
print(fun1(1,2))
print(fun2(1,2))
my_func = lambda x: "优秀" if x > 80 else "差评"
res1 = my_func(1)
print(res1) # "差评"
res2 = my_func(100)
print(res2) # "优秀"
def fun1():
print(1)
yield 3
print(2)
yield 4
def my_fun(): print(123) yield 22 print(456) yield 33 print(789) yield 44 print(101112) my_generator = my_fun() # my_fun生成器函数遇到next开始执行,把yield后的值返回给res1 res1 = next(my_generator) print(res1) # 22 res2 = next(my_generator) print(res2) # 33 res3 = next(my_generator) print(res3) # 44 res4 = next(my_generator) print(res4) # 结束,程序报错误StopIteration
items= my_fun()
for item in items:
print(item)
一次性生成所有随机数,放到数组待用
import random
list1 = []
for i in range(200000000):
res = random.randint(100, 999)
list1.append(res)
用yield生成随机数,用多少取多少,节省空间
import random
def random_num(counts):
count = 0
while count < counts:
yield random.randint(100, 999)
count += 1
list1 = random_num(2000000)
.send()括号中的值,赋给yield“=”前的变量
def my_function(): print(111) val1 = yield 1 print('val1:', val1) print(222) val2 = yield 2 print('val2:',val2) print(333) val3 = yield 3 print('val3:',val3) print(444) res = my_function() gen1 = res.send(None) print(gen1) gen2 = res.send(666) print(gen2) gen3 = res.send(777) print(gen3) gen4 = res.send(888) print(gen4)
字体
# abs,绝对值 a = abs(-5) # pow,指数,类似3**4 b1 = pow(3, 4) print(b1) # sum,求和 c1 = sum([-6, 7, 8, 9, 10]) print(c1) # divmod,取商和余数 d1, d2 = divmod(11, 2) print(d1, d2) # round,小数点后n位,多的四舍五入 e1 = round(4.11786, 2) print(e1) # 4.12
# min,最小值
a1 = min(1, 2, 3, 4, 5, 6)
print(a1) # 1
a2 = min([1, 2, 3, 4, 5, 6]) # 列表
print(a2)
a3 = min([-1, 2, 3, 4, 5, 6], key=lambda x: abs(x))
print(a3) # -1
a1 = max(1, 2, 3, 4, 5, 6)
print(a1)
a2 = max([1, 2, 3, 4, 5, 6])
print(a2)
a3 = max([-1, 2, 3, 4, 5, 6], key=lambda x: x * 10)
print(a3) # 6
a1 = all( [1,2,3,""] ) # False
v2 = any([1,2,3,""]) # True
# ord,取字符的unicode码,用十进制表示
a1 = ord("张")
print(a1, hex(a1))
# chr,根据十进制的编码获取字符
a1 = chr(24352)
print(a1)
后加()即可相互转化
int
foat
str,unicode编码
bytes,utf-8、gbk编码
a1 = "张三"
a2 = a1.encode('utf-8') # bytes
print(a2)
a3 = bytes(a1,encoding="utf-8") # bytes
print(a3)
bool
list
dict
tuple
set
len取长度
print打印
input输入
open打开文件
type获取类型
range 返回一个迭代对象
enumerate
a1 = ["张三", "李四", '王五']
# 8表示从8开始做序号
for id, item in enumerate(a1, 8):
print(id, item)
# 8 张三
# 9 李四
# 10 王五
id取地址
hash取哈希值
zip
a1 = [1, 2, 3, 4, 5, 6]
a2 = [1, 2, 3, 4]
a3 = [9, 8, 7, 6, 5]
res = zip(a1, a2, a3)
for item in res:
print(item) #(1, 1, 9)
#(2, 2, 8)
#(3, 3, 7)
#(4, 4, 6)
a1 = "张三"
a2 = lambda x: x
def a3():
pass
print( callable(a1) ) # False
print(callable(a2)) # True
print(callable(a3)) # True
a1 = sorted([9,2,7,4,5,6])
print(a1) # [2, 4, 5, 6, 7, 9]
info = { "user1": { 'id': 55, 'age': 112 }, "user2": { 'id': 32, 'age': 67 }, "user3": { 'id': 23, 'age': 99 }, "user4": { 'id': 41, 'age': 29 }, } # 按id值进行排序 res = sorted(info.items(), key=lambda x: x[1]['id']) print(res)
data_list = [
'1-5 上海滩.mp3',
'1-17 我的祖国.mp3',
'1-9 梦回大唐.mp3',
]
# 按1-4后面的数字排序,“4”的位置
res = sorted(data_list, key=lambda x: int(x.split(' ')[0].split("-")[-1]) )
print(res)
list1 = [ i for i in range(12)]
list2 = [ [i,i] for i in range(12) if i > 6 ]
集合的元素和字典的键满足可哈希
set1 = { n for n in range(10)}
print(set1) # {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
set2 = { (n,n,n) for n in range(10) if n>3}
print(set2) # {(6, 6, 6), (5, 5, 5), (4, 4, 4), (9, 9, 9), (7, 7, 7), (8, 8, 8)}
dic1 = { n:n for n in range(9)}
dic1 = { n:(n,3) for n in range(9)}
dic1 = { n:(n,5) for n in range(19) if n>7}
data1 = (v for v in range(9))
print(data1)
for i in data1:
print(i)
# 元组,返回生成器对象
def function1():
return (lambda x: i * x * 3 for i in range(4))
# for循环返回值,执行返回值函数(2)
res = [fun(2) for fun in function1()] # [0,6,12,18 ]
print(res)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。