Lambda表达式
python 使用 lambda 表达式来创建匿名函数
foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
能将 filter( 函数,列表,元组,字典,可迭代的)
print( list(filter(lambdax: x % 3 == 0, foo)))
print (list(filter(lambdax: x % 3 == 0, foo)))
print (list(map(lambdax: x * 2 + 10, foo)))
import functools,_functools
def max(x,y):
if x<y:
return x
elif y<x:
return y
else:
return x
print( functools.reduce(max ,foo))
a=int( input('请输入第一个操作数'))
b=int(input('请输入第一个操作数'))
fun1=input('请输入要执行的函数:')
fun1=eval(fun1)
print(fun1(a,b))
闭包和装饰器
nonlocal 声明变量位置是 闭包
def A():
num=10
def B():
nonlocal num
num=num+5
return num
return B
a=A()
print(a)
print(a.__name__)
print(a())
def funx(x):
def funy(y):
return x*y
return funy
x=funx(5)
ret= x(6)
ret1= x(7)
print(ret)
print(ret1)
def test(a,b,fun):
n= fun(a,b)
return n
lam=input('请输入一个函数')
t=test(10,5,eval(lam))
print( t)
def outter(fun):
def inner():
print('inner()---123456')
fun()
return inner
@outter
def index():
print('index()-----')
index()
学生出门上课
def waitao(fun):
print('穿上外套')
print(fun)
return fun
def kuzi(fun):
print('穿上裤子')
print(fun)
return fun
def xiezi(fun):
print('穿上鞋子')
print(fun)
return fun
def benben(fun):
print('带上笔记本')
print(fun)
return fun
@benben
@kuzi
@xiezi
@waitao
def student():
print('学生出门去上课')
student()
冒泡排序;
L=[12,5,25,3,62,555,4,25,5]
升序 降序
两两相比 小靠前
外侧循环n-1
内侧循环n-1-i
for i in range(len(L)-1):
for j in range(len(L)-1-i):
if L[j]>L[j+1]:
L[j],L[j+1]=L[j+1],L[j]
L.insert(7,30)
for i in range(len(L)):
if L[i]>30:
L.insert(i,30)
break
print(L)