1.获取移动平均值

def wrapper(fun):
def inner(*args,**kwargs):
ret=fun(*args,**kwargs)
ret.__next__()
return ret
return inner
@wrapper
def g():
sum=0
count=0
avg=0
while True:
num=yield avg
sum+=num
count+=1
avg=sum/count
f=g()
print(f.send(10))
print(f.send(20))
print(f.send(30))

2.处理文件:用户指定要查找的文件和内容
将文件中包含要查找内容的每一行都输出到屏幕

def search():
with open("a1.txt",encoding="utf-8") as f1:
for i in f1:
if "我" in i:
yield i
g=search()
for i in g:
print(i.strip())

3.写生成器,从文件中读取内容,在每一次读取到的内容之前加上‘***’之后再返回用户

def file_add():
with open("a1.txt",encoding="utf-8") as f1:
for i in f1:
yield "***"+i
g=file_add()
for i in g:
print(i.strip())

4.面试题

def demo():
for i in range(4):
yield i g=demo() g1=(i for i in g)
g2=(i for i in g1) print(list(g1))
print(list(g2)) 面试题1
def add(n,i):
return n+i def test():
for i in range(4):
yield i g=test()
for n in [1,10]:
g=(add(n,i) for i in g) print(list(g))
import os

def init(func):
def wrapper(*args,**kwargs):
g=func(*args,**kwargs)
next(g)
return g
return wrapper @init
def list_files(target):
while 1:
dir_to_search=yield
for top_dir,dir,files in os.walk(dir_to_search):
for file in files:
target.send(os.path.join(top_dir,file))
@init
def opener(target):
while 1:
file=yield
fn=open(file)
target.send((file,fn))
@init
def cat(target):
while 1:
file,fn=yield
for line in fn:
target.send((file,line)) @init
def grep(pattern,target):
while 1:
file,line=yield
if pattern in line:
target.send(file)
@init
def printer():
while 1:
file=yield
if file:
print(file) g=list_files(opener(cat(grep('python',printer())))) g.send('/test1') 协程应用:grep -rl /dir tail&grep

python练习题-day13的更多相关文章

  1. Python练习题 028:求3*3矩阵对角线数字之和

    [Python练习题 028] 求一个3*3矩阵对角线元素之和 ----------------------------------------------------- 这题解倒是解出来了,但总觉得 ...

  2. Python练习题 027:对10个数字进行排序

    [Python练习题 027] 对10个数字进行排序 --------------------------------------------- 这题没什么好说的,用 str.split(' ') 获 ...

  3. Python练习题 026:求100以内的素数

    [Python练习题 026] 求100以内的素数. ------------------------------------------------- 奇怪,求解素数的题,之前不是做过了吗?难道是想 ...

  4. Python练习题 025:判断回文数

    [Python练习题 025] 一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. ---------------------------------------- ...

  5. Python练习题 024:求位数及逆序打印

    [Python练习题 024] 给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. ---------------------------------------------- ...

  6. Python练习题 004:判断某日期是该年的第几天

    [Python练习题 004]输入某年某月某日,判断这一天是这一年的第几天? ---------------------------------------------- 这题竟然写了 28 行代码! ...

  7. Python练习题-1.使用匿名函数对1~1000求和,代码力求简洁。

    Python 练习 标签(空格分隔): Python Python练习题 Python知识点 一.使用匿名函数对1~1000求和,代码力求简洁. 答案: In [1]: from functools ...

  8. PYTHON练习题 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数。

    Python 练习 标签: Python Python练习题 Python知识点 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数,如果大于预设的 ...

  9. python 基础 2.8 python练习题

    python 练习题:   #/usr/bin/python #coding=utf-8 #@Time   :2017/10/26 9:38 #@Auther :liuzhenchuan #@File ...

随机推荐

  1. Apache Spark as a Compiler: Joining a Billion Rows per Second on a Laptop(中英双语)

    文章标题 Apache Spark as a Compiler: Joining a Billion Rows per Second on a Laptop Deep dive into the ne ...

  2. Atitit 管理的模式扁平化管理 金字塔 直线型管理 垂直管理 水平管理 矩阵式管理 网状式样管理 多头管理 双头管理

    Atitit 管理的模式扁平化管理  金字塔 直线型管理 垂直管理 水平管理 矩阵式管理 网状式样管理 多头管理 双头管理 1.1. 矩阵管理 1 1.2. 相关信息 矩阵的历史 1 1.3. 基于“ ...

  3. idea maven 集成多模块 module

    首先第一步创建 顶级项目  也就是父项目 在创面那部中 不管你勾不勾 create from 那个选项 都无所谓,最终创建的项目要全删的 ,只保留pom.xml 父项目结构 接下来 创建子项目  也是 ...

  4. MongoDB 查询总结

    1.含日期查询 从起始时间到结束时间 BasicDBObject queryObj = new BasicDBObject(); queryObj.put("date",new B ...

  5. 外盘持仓盈亏何时推送---ITapTradeAPINotify::OnRtnPositionProfit

    易盛外盘提供了一个可以直接获取持仓盈亏的函数,这个比CTP方便多了 virtual void TAP_CDECL ITapTrade::ITapTradeAPINotify::OnRtnPositio ...

  6. vue 版本升级配置修改

  7. 【转】Eclipse 乱码 解决方案总结(UTF8 -- GBK)

    转载自: http://www.cnblogs.com/bluestorm/archive/2012/09/20/2695567.html UTF8 --> GBK;   GBK --> ...

  8. hdoj:2075

    A|B? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. Android 8 设置蓝牙名称 流程

    记录android 8设置蓝牙名称的流程. packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothDeviceRenam ...

  10. opencv利用Cascade Classifier训练人脸检测器

    opencv默认提供了haar特征和lbp特征训练的人脸分类器,但是效果不太好,所以我们可以用opencv提供的跑opencv_traincascade函数来训练一个LBP特征的分类器.(由于open ...