首页 > 代码库 > Python基础(递归、模块、包以及正则)-day05
Python基础(递归、模块、包以及正则)-day05
写在前面
上课第四天,打卡:
如果再见不能红着眼,是否还能红着脸;
一、协程函数(生成器:yield的表达式形式)
1.yield 的语句形式: yield 1
2.yield 的表达式形式: x=yield
注意:next(g) #等同于 g.send(None),示例如下:
1 def deco(func): 2 def wrapper(*args,**kwargs): 3 res=func(*args,**kwargs) 4 next(res) 5 return res 6 return wrapper 7 8 @deco 9 def eater(name):10 print(‘%s ready to eat‘ %name)11 food_list=[]12 while True:13 food=yield food_list14 food_list.append(food)15 print(‘%s start to eat %s‘ %(name,food))16 17 18 g=eater(‘alex‘)19 # print(g)20 # next(g) #等同于 g.send(None)
1 # yield表达式形式之练习一 2 def deco(func): 3 def wrapper(*args,**kwargs): 4 res = func(*args,**kwargs) 5 next(res) 6 return res 7 return wrapper 8 9 @deco10 def eater(name):11 print(‘%s ready to eat‘ % name)12 while True:13 food=yield14 print("%s start to eat %s" % (name, food))15 g = eater(‘alex‘)16 next(g)
1 # yield表示器形式之练习二 2 def deco(func): 3 def wrapper(*args,**kwargs): 4 res = func(*args,**kwargs) 5 next(res) 6 return res 7 return wrapper 8 9 @deco10 def eater(name):11 print(‘%s ready to eat‘ % name)12 food_list = []13 while True:14 food=yield15 food_list.append(food)16 print("%s start to eat %s" % (name, food))17 print("%s have eaten: %s" % (name,food_list))18 g = eater(‘alex‘)19 g.send(‘tomato‘)20 g.send(‘potato‘)21 g.send(‘beef‘)22 g.send(‘rice‘)
3.yield表达式形式的应用
1 #!/usr/bin/python 2 # -*- coding:utf-8 -*- 3 4 # 实现Linux下的如下命令: grep -rl ‘python‘ path 5 # 即给出一个目录和patten,递归遍历查找该目录下含有该patten的文件完整路径,如果文件含有多行则需要去重; 6 7 import os 8 9 def init(func):10 def wrapper(*args,**kwargs):11 res = func(*args,**kwargs)12 next(res)13 return res14 return wrapper15 @init16 def search_all_file(target):17 while True:18 path = yield19 g = os.walk(path)20 for path,_,files in g:21 for file in files:22 target.send(r‘%s\%s‘ % (path,file))23 @init24 def opener(target):25 while True:26 file_to_open = yield27 with open(file_to_open,encoding=‘utf-8‘) as rf:28 for line in rf:29 res = target.send((file_to_open,line))30 if res:31 break32 @init33 def grep(patten):34 flag = False35 while True:36 the_file,line = yield flag37 flag = False38 if patten in line:39 flag = True40 print(the_file)41 42 path = r‘D:\soft\work\Python_17\day05\a‘43 g = search_all_file(opener(grep(‘python‘)))44 print(g)45 g.send(path)
二、递归调用
1.递归的概念
在函数调用过程中,直接或间接地调用了函数本身,这就是函数的递归调用;
包括两部分:递推和回溯;
1 def f1():2 print(‘from f1‘)3 f1()4 5 f1()
2.递归的层数限制
1 import sys2 print(sys.getrecursionlimit()) # 查看默认递归次数上限3 sys.setrecursionlimit(2000) # 认为设置递归上限4 print(sys.getrecursionlimit())5 6 ---7 10008 2000
3.递归特性
1. 必须有一个明确的结束条件;
2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减少;
3. 递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧;每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出);
4.堆栈扫盲:http://www.cnblogs.com/lln7777/archive/2012/03/14/2396164.html
5.尾递归优化:http://egon09.blog.51cto.com/9161406/1842475
4.递归具体应用
1.二分法查找某个元素
前提:输入的可循环对象必须是有序的;
1 l = [1, 2, 10,33,53,71,73,75,77,85,101,201,202,999,11111] 2 3 def search(find_num,seq): 4 if len(seq) == 0: 5 print(‘not exists‘) 6 return 7 mid_index=len(seq)//2 8 mid_num=seq[mid_index] 9 print(seq,mid_num)10 if find_num > mid_num:11 #in the right12 seq=seq[mid_index+1:]13 search(find_num,seq)14 elif find_num < mid_num:15 #in the left16 seq=seq[:mid_index]17 search(find_num,seq)18 else:19 print(‘find it‘)
2.DNS 的递归和迭代查找
待整理
3.Linux深水炸弹
待整理
三、内置函数补充
1.匿名函数lambda
2.max 和 min
3.sorted
4.zip
5.map
6.reduce
7.filter
8.global
1 x=1000 2 def f1(): 3 global x 4 x=0 5 6 f1() 7 print(x) 8 9 ---10 0
四、面向过程编程与函数式编程
1.面向过程编程
1.概念:面向过程的程序设计:是一种流水线式的、机械式的过程;
2.优缺点
1.优点
程序结构清晰,复杂的问题分解成多个简单的最小单元,每个单元完成各自的功能;
2.缺点
扩展性差,牵一发而动全身;
3.应用场景
Linux内核、git、httpd
2.函数式编程
1.函数式编程(不同于面向过程编程),是模拟数学意义上的函数;
2.特性
1.不允许对外部变量做任何修改
2.没有循环的概念,所有的循环都是用 尾递归 实现的;
3.函数式编程语言 与 Python无关;
...
五、模块的使用
1.import ... 导入
1.产生新的名称空间;
2.以新建的名称空间为全局名称空间,执行文件的代码;
3.拿到一个模块名spam,指向spam.py产生的名称空间;
2.from ... import ... 导入
1.产生新的名称空间;
2.以新建的名称空间为全局名称空间,执行文件的代码;
3.直接拿到就是spam.py产生的名称空间中名字;
1 from ... import ...2 优点:方便,不用加前缀3 缺点:容易跟当前文件的名称空间冲突
3.模块搜索路径
1.内存
2.内置
3.sys.path
1 import spam ===>> 就是要找当前路径下的 spam.py 这个文件 2 3 4 如果在其他级别的路径下,则需要把该路径加入到 sys.path 5 6 import sys 7 sys.path.append(r‘path‘) 8 sys.path.insert(0,r‘path‘) 9 10 或者把相关的py文件移动到已有的sys.path某个路径下
参考:http://www.cnblogs.com/linhaifeng/articles/6379069.html
六、包的使用
参考:http://www.cnblogs.com/linhaifeng/articles/6379069.html
七、正则相关
import re
参考:http://www.cnblogs.com/linhaifeng/articles/6384466.html#_label11
这部分需要都整理一遍
other
高阶函数
第一类对象
作业:
1 函数+正则===》计算器2 -1+(1+2*3-(-4/3)+10.3/2)
Python基础(递归、模块、包以及正则)-day05