一.有以下员工信息表
当然此表你在文件存储时可以这样表示
1 1,Alex Li,22,13651054608,IT,2013-04-01
现需要对这个员工信息文件,实现增删改查操作
1.可进行模糊查询,语法至少支持下面3种:
select name,age from staff_table where age > 22
select from staff_table where dept = “IT”
select from staff_table where enroll_date like “2013”
查到的信息,打印后,最后面还要显示查到的条数
2.可创建新员工纪录,以phone做唯一键,staff_id需自增
3.可删除指定员工信息纪录,输入员工id,即可删除
4.可修改员工信息,语法如下:
UPDATE staff_table SET dept=“Market” WHERE where dept = “IT”
注意:以上需求要充分使用函数,请尽最大限度来减少重复代码!
二.用map来处理字符串列表啊,把列表中所有人都变成sb,如alex_sb
name=['alex','wupeiqi','yuanhao']
name_sb=map(lambda x: x+"_sb",name)
print(list(name_sb))
用map来处理下述l,然后用list得到一个新的列表,列表中每个人的名字都是sb结尾
l=[{'name':'alex'},{'name':'y'}]
l_sb=map(lambda x: x['name']+"_sb",l)
print(list(l_sb))
三.用filter来处理,得到股票价格大于20的股票名字
shares={'IBM':36.6,'Lenovo':23.2,'oldboy':21.2,'ocean':10.2}
l=filter(lambda x:shares[x]>20,shares)
print(list(l)) #结果为['IBM','Lenovo','oldboy']
#遍历每一个元素,此时元素为list的每个key
四.portfolio = [{‘name’: ‘IBM’, ‘shares’: 100, ‘price’: 91.1},{‘name’: ‘AAPL’, ‘shares’: 50, ‘price’: 543.22},{‘name’: ‘FB’, ‘shares’: 200, ‘price’: 21.09},{‘name’: ‘HPQ’, ‘shares’: 35, ‘price’: 31.75},{‘name’: ‘YHOO’, ‘shares’: 45, ‘price’: 16.35},{‘name’: ‘ACME’, ‘shares’: 75, ‘price’: 115.65}]
1.用map来得出一个包含数字的迭代器,数字指的是:购买每支股票的总价格
portfolio = [{'name': 'IBM', 'shares': 100, 'price': 91.1},{'name': 'AAPL', 'shares': 50, 'price': 543.22},{'name': 'FB', 'shares': 200, 'price': 21.09},{'name': 'HPQ', 'shares': 35, 'price': 31.75},{'name': 'YHOO', 'shares': 45, 'price': 16.35},{'name': 'ACME', 'shares': 75, 'price': 115.65}]
p=map(lambda x:x['shares']*x['price'],portfolio)
print(list(p)) #结果为[9110.0, 27161.0, 4218.0, 1111.25, 735.75000 00000001, 8673.75]
2.基于1的结果,用reduce来计算,购买这些股票总共花了多少钱
p=[9110.0, 27161.0, 4218.0, 1111.25, 735.75000 00000001, 8673.75]
pt=reduce(lambda x,y:x+y,list(p))
print(pt) #结果为51009.75
3.用filter过滤出,单价大于100的股票有哪些
portfolio = [{'name': 'IBM', 'shares': 100, 'price': 91.1},{'name': 'AAPL', 'shares': 50, 'price': 543.22},{'name': 'FB', 'shares': 200, 'price': 21.09},{'name': 'HPQ', 'shares': 35, 'price': 31.75},{'name': 'YHOO', 'shares': 45, 'price': 16.35},{'name': 'ACME', 'shares': 75, 'price': 115.65}]
up=filter(lambda x:x['price']>100,portfolio)
print(list(up))
#结果为[{'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'ACME', 'shares': 75, 'price': 115.65}]
五.自定义函数模拟range(1,7,2)
def my_range(start,stop,step):
while start < stop:
yield start
start+=step
#执行函数得到生成器,本质就是迭代器
obj=my_range(1,7,2) #1 3 5
print(next(obj))
print(next(obj))
print(next(obj))
print(next(obj)) #StopIteration
#应用于for循环
for i in my_range(1,7,2):
print(i)
六.模拟管道,实现功能:tail -f access.log | grep ‘404’
import time
def tail(filepath):
with open(filepath,'rb') as f:
f.seek(0,2)
while True:
line=f.readline()
if line:
yield line
else:
time.sleep(0.2)
def grep(pattern,lines):
for line in lines:
line=line.decode('utf-8')
if pattern in line:
yield line
for line in grep('404',tail('access.log')):
print(line,end='')
#测试
with open('access.log','a',encoding='utf-8') as f:
f.write('出错啦404\n')
七.编写装饰器,实现初始化协程函数的功能
def init(func):
def wrapper(*args,**kwargs):
g=func(*args,**kwargs)
next(g)
return g
return wrapper
@init
def eater(name):
print('%s 准备开始吃饭啦' %name)
food_list=[]
while True:
food=yield food_list
print('%s 吃了 %s' % (name,food))
food_list.append(food)
g=eater('egon')
g.send('蒸羊羔')
八.实现功能:grep -rl ‘python’ /etc
#注意:target.send(...)在拿到target的返回值后才算执行结束
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:
filepath=yield
g=os.walk(filepath)
for dirname,_,files in g:
for file in files:
abs_path=r'%s\%s' %(dirname,file)
target.send(abs_path)
@init
def opener(target):
while True:
abs_path=yield
with open(abs_path,'rb') as f:
target.send((f,abs_path))
@init
def cat(target):
while True:
f,abs_path=yield
for line in f:
res=target.send((line,abs_path))
if res:
break
@init
def grep(pattern,target):
tag=False
while True:
line,abs_path=yield tag
tag=False
if pattern.encode('utf-8') in line:
target.send(abs_path)
tag=True
@init
def printer():
while True:
abs_path=yield
print(abs_path)
g=search(opener(cat(grep('你好',printer()))))
# g.send(r'E:\CMS\aaa\db')
g=search(opener(cat(grep('python',printer()))))
g.send(r'E:\CMS\aaa\db')