首页 > 代码库 > Day 23 (06/20)匿名函数、递归
Day 23 (06/20)匿名函数、递归
内置函数补充:
# print(divmod(73,23))
#
# total_count=73
# per_count=23
#
# res=divmod(total_count,per_count)
# if res[1] > 0:
# page_count=res[0]+1
#
# print(page_count)
#
#
# l=[‘a‘,‘b‘,‘c‘]
#
#
# for i in enumerate(l):
# print(i)
#
# dic={‘name‘:‘egon‘,‘age‘:18}
# for i in enumerate(dic):
# print(i)
# s=set({1,2,3})
# s.add(4)
#
# s.pop()
# print(s)
s=frozenset({1,2,3})
# print(globals() is locals())
# s1=‘hello123123‘
# s2=‘ hello123123‘
#
# print(hash(s1))
# print(hash(s2))
# print(type(1))
# print(isinstance(1,int))
# iter([1,2,3]) #[1,2,3].__iter__()
# print(max([1,2,3,4,5]))
# print(
# max(
# i for i in range(10)
# )
# )
#
# print(pow(3,2,2)) #3**2%2
# for i in range(0,5):
# print(i)
# for i in range(-5,0):
# print(i)
#
# for i in range(0,5,2): #0 2 4
# print(i)
# for i in range(5,0,-1): #0 2 4
# print(i)
# l=[‘a1‘,‘a2‘,‘a3‘,‘a4‘,‘a5‘,‘a6‘,‘a7‘,‘a8‘,‘a9‘]
# # print(l[2:5:2])
# # print(l[::2])
# print(l[::-1])
# l=[‘a1‘,‘a2‘,‘a3‘,‘a4‘,‘a5‘,‘a6‘,‘a7‘,‘a8‘,‘a9‘]
# l1=[3,5,1,2]
# print(list(reversed(l1)))
# print(l1)
# l1=list(reversed(l1))
#
# print(round(3.3456,3))
# l=[‘a1‘,‘a2‘,‘a3‘,‘a4‘,‘a5‘,‘a6‘,‘a7‘,‘a8‘,‘a9‘]
# # print(l[2:5:2])
#
# x=slice(2,5,2)
# print(l[x])
#
# l1=[1,2,3,4,5]
# print(l1[x])
# print(sum(i for i in range(101)))
# print(sum([1,2,3,4]))
# print(sum([‘a1‘,‘a2‘]))
# vars()
# s=‘hello‘
# l=[1,2,3]
#
# for i in zip(s,l):
# print(i)
#复习:
# name=‘alex‘
# name=‘egon‘
# res=‘SB‘ if name == ‘alex‘ else ‘shuai‘
# print(res)
# egg_list=[‘鸡蛋%s‘ %i for i in range(10)] #列表解析
# print(egg_list)
#
#
# egg_g=(‘鸡蛋%s‘ %i for i in range(10)) #列表解析
# print(egg_g)
# s=‘hello‘
# print([True for i in s if i == ‘e‘])
# moneys=[]
# with open(‘goods‘,encoding=‘utf-8‘) as f:
# for line in f:
# # print(line)
# l=line.split()
# price=float(l[1])
# count=int(l[2])
# moneys.append(price * count)
#
# print(moneys)
# print(sum(moneys))
# with open(‘goods‘,encoding=‘utf-8‘) as f:
# # l=[line.split() for line in f]
# # l1=[float(item[1])*int(item[2]) for item in l]
# # print(l1)
# l=[float(line.split()[1]) * int(line.split()[2]) for line in f]
# print(sum(l))
# with open(‘goods‘,encoding=‘utf-8‘) as f:
# # g=(float(line.split()[1]) * int(line.split()[2]) for line in f)
# # print(sum(g))
# print(sum(float(line.split()[1]) * int(line.split()[2]) for line in f))
# with open(‘goods‘,encoding=‘utf-8‘) as f:
# g=(float(line.split()[1]) * int(line.split()[2]) for line in f)
# print(sum(g))
# g=(float(line.split()[1]) * int(line.split()[2]) for line in open(‘goods‘,encoding=‘utf-8‘))
# print(sum(g))
#模拟数据库查询
# goods_info=[]
#
# with open(‘goods‘,encoding=‘utf-8‘) as f:
# for line in f:
# l=line.split()
# name=l[0]
# price=l[1]
# count=l[2]
# goods_info.append({‘name‘:name,‘price‘:price,‘count‘:count})
#
# print(goods_info)
# goods_info=[]
with open(‘goods‘,encoding=‘utf-8‘) as f:
# for line in f:
# l=line.split()
# goods_info.append({‘name‘:l[0],‘price‘:l[1],‘count‘:l[2]})
goods_info=[{‘name‘:line.split()[0],‘price‘:float(line.split()[1]),‘count‘:int(line.split()[2])} for line in f \
if float(line.split()[1]) > 3000]
print(goods_info)
匿名函数
# def func1(x):
# return x**2
# print(func1)
# func2=lambda x:x**2 #return print(‘hello‘)
# print(func2)
# print(func2(2))
# def func1(x):
# return x**2
#
# func1(2)
# del func1
# f=lambda x:x**2
# print(f(2))
#
# f1=lambda x,y:x+y
# print(f1(1,2))
#
# f2=lambda x:x>1
# print(f2(3))
salaries={
‘egon‘:3000,
‘alex‘:100000000,
‘wupeiqi‘:10000,
‘yuanhao‘:2000
}
# max
# print(max(salaries))
# print(max(salaries.values()))
# res=zip(salaries.values(),salaries.keys())
# # for i in res:
# # print(i)
#
# print(max(res)[-1])
salaries={
‘egon‘:3000,
‘alex‘:100000000,
‘wupeiqi‘:10000,
‘yuanhao‘:2000
}
# def func(x):
# return salaries[x]
# print(max(salaries,key=lambda x:salaries[x]))
# min
# zip
# map:映射
# l=[1,2,3,4]
# m=map(lambda x:x**2,l)
# print(list(m))
# names=[‘alex‘,‘wupeiqi‘,‘yuanhao‘]
# print(list(map(lambda item:item+‘_SB‘,names)))
#reduce:合并
from functools import reduce
# res=0
# for i in range(100):
# res+=1
# print(reduce(lambda x,y:x+y,range(100),100))
# filter:过滤
#
# names=[‘alex_sb‘,‘yuanhao_sb‘,‘wupeiqi_sb‘,‘egon‘]
# print(list(filter(lambda name:name.endswith(‘_sb‘),names)))
#sorted
l=[1,2,4,9,-1]
print(sorted(l)) #从小到大
print(sorted(l,reverse=True)) #从大到小
递归
‘‘‘
递归调用:
在调用一个函数的过程中,直接或者间接调用了该函数本身
‘‘‘
# def func():
# print(‘====>func‘)
# func()
#
# func()
# def foo():
# print(‘from foo‘)
# bar()
#
# def bar():
# print(‘from bar‘)
# foo()
#
# foo()
# age(1)=age(2)+2
# age(2)=age(3)+2
# age(3)=age(4)+2
# age(4)=age(5)+2
# age(5)=18
#
#
#
# age(n)=age(n+1)+2 #n<5
# age(n)=18 #n=5
# def age(n):
# if n == 5:
# return 18
# return age(n+1)+2
#
#
# print(age(1))
l=[1,[2,3,[4,5,[6,7,[8,9,[10,11,[12,13]]]]]]]
def func(l):
for i in l:
if isinstance(i,list):
func(i)
else:
print(i)
func(l)
Day 23 (06/20)匿名函数、递归