首页 > 代码库 > day05匿名函数,内置函数,二分法,递归,模块

day05匿名函数,内置函数,二分法,递归,模块

yield作为表达式来使用的方式

#grep -rl ‘python /root
"""
查找root下文件中含有python的文件
"""
import os
def init(func):
    def wrapper(*args,**kwargs):
        g=func(*args,**kwargs)
        next(g)
        return g
    return wrapper
@init
def search(target):

    while True:
        search_path = yield
       
g=os.walk(search_path)
        # print(g)
       
for par_dir,dirnames,files in g:#g的属性dirpath, dirnames该目录下的文件夹, filenames
           
for file in files:
                file_abs_path=r‘%s\%s‘ %(par_dir,file)
                # print(file_abs_path)
               
target.send(file_abs_path)
@init
def openner(target):
    while True:
        open_path = yield
        with
open(open_path,encoding=‘utf-8‘) as f:
            target.send((open_path,f))#双括号是以元组形式传送了一个值
@init
def cat(target):
    while True:
        open_path,f=yield
        for
line in f:
            tag=target.send((open_path,line))
            if tag:
                break
@init
def grep(target,word):
    tag = False
    while True
:
        open_path,line = yield tag
        tag=False#发送完成就重置为False,否则后面的文件都不会再正常运行
        if
word in line:
            tag=True
           
target.send((open_path))
@init
def printer():
    while True:
        open_path = yield
       
print((open_path))


g=search(openner(cat(grep(printer(),‘python‘))))
# print(g)
g.send(r‘D:\kinggsoft\python_file\day05\a‘)

‘‘‘

面向过程的程序设计:是一种流水线式的编程思路,是机械式

优点:

    程序的结构清晰,可以把复杂的问题简单

 

缺点:

    1 扩展性差

 

 

应用场景:

    1 linux内核,git,httpd

 

‘‘‘

匿名函数lambda

# def func(x,y):

#     return x+y

# func(1,2)

a=lambda x,y,z:x+y+z

print(a(1,2,3))

print(a)è <function <lambda> at 0x0000000001171400>

这样是没有意义的,通常用在不需要名字的地方

max、min、sorted、zip

salaries={‘alex‘:1000,
          ‘dodo‘:300000,
          ‘egon‘:2000,
          ‘yric‘:200
          }
# (max(salaries))#这里比较的是key的值的首字母的大小
# res=zip(salaries.values(),salaries.keys())#
# print(list(zip))
# print(max(res))
print(max(salaries,key=lambda x:salaries[x]))
print(min(salaries,key=lambda x:salaries[x]))
print(sorted(salaries,key=lambda x:salaries[x]))

 

map、reduce、filter

map:

l=[‘alex‘,‘wupeiqi‘,‘yuanhao‘]

res=map(lambda x:x+‘_SB‘,l)

print(list(res))

print(res)

输出结果:

[‘alex_SB‘, ‘wupeiqi_SB‘, ‘yuanhao_SB‘]

<map object at 0x0000000000A90400>

reduce:

reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])è    ((((1+2)+3)+4)+5).

from functools import reduce

l=[‘1‘,‘2‘,‘3‘,‘4‘,‘5‘]

print(reduce(lambda x,y:x+y,l,‘a‘))è a12345

filter:

l=[‘1‘,‘2‘,‘3‘,‘4‘,‘5‘]

# print(reduce(lambda x,y:x+y,l,‘a‘))

l=list(map(lambda x:x+‘a‘,l))

res=filter(lambda x:x.endswith(‘a‘),l)

print(res)

print(list(res))

输出结果:输出以a结尾的l中的所有元素

<filter object at 0x00000000011904E0>

[‘1a‘, ‘2a‘, ‘3a‘, ‘4a‘, ‘5a‘]

二分法

#二分法
l=[1,3,4,5,3,5,6,2]
l.sort()
# print(l)
word=4
while True :
    max_item = len(l)-1
    min_item = 0
    mid_item = int((max_item-min_item)/2)+1
    # print(l[mid_item])
   
if max_item>min_item:
        if word > l[mid_item]:
            l=l[mid_item:max_item]
        elif word < l[mid_item]:
            l=l[min_item:mid_item]
        else:
            print(‘word is finded‘)
            break
    else
:
        print(‘not find‘)
        break

找到最大值和最小值,在找到中间值,和中间值进行比较,用切片来缩小范围

递归调用

递归调用:在函数调用过程中,直接或间接地调用了函数本身,这就是函数的递归调用

def age(n):

    if n == 1:

        return 18

    return age(n-1)+2

 

print(age(5))

 

day05匿名函数,内置函数,二分法,递归,模块