初次编辑2017年10月28日,星期六 python
引用:百度数据库
def func(): n=0 while True: yield n n += 1 g=func() res=next(g) for i in g: pass
def eater(name): print('%s start to eat '%name) while True: food=yield print('%s eat %s'%(name,food)) e=eater('zhejiangF4')
def foo(func): def foo1(*args,**kwargs): res = func(*args,**kwargs) next(res) return res #此处理解 生成器是一次性的 return foo1 @foo #eater = foo(eater) = foo1 def eater(name): print('%s start to eat food '% name) food_list = [] while True: food = yield food_list print('%s get %s, to start eat '% (name, food)) food_list.append(food) print('Done') e = eater('钢蛋') #foo1('钢蛋') print(e.send('123'))
from urllib.request import urlopen def my_next(func): def foo(*args,**kwargs): res = func(*args,**kwargs) next(res) return res return foo @my_next def get(): while True: url = yield res = urlopen(url).read() #爬网页返回值 print(res) #输出爬网页结果 g=get() g.send('http://www.baidu.com') g.send('http://www.python.org')
# grep -rl 'python' C:\egon import os #装饰器,将生成器next初始化 def init(func): def foo(*args,**kwargs): res = func(*args,**kwargs) next(res) return res return foo @init def search(target): '查找文件绝对路径' while True: dir_path = yield #此yield放在while外仍是内有疑问:yield放在while外就会形成死循环,没法进行下次yield g = os.walk(dir_path) #g为迭代器 for i in g: # print(i) for j in i[-1]: file_path = '%s\\%s'%(i[0],j) target.send(file_path) @init def opener(target): '打开文件获取文件句柄' while True: file_path = yield with open(file_path) as f: target.send((f,file_path)) #send传递两个文件时,需加括号 @init def cat(target): '读取文件内容' while True: f,file_path = yield for line in f: #读取一行文件 target.send((line,file_path)) @init def grep(target,pattern): #传递两个参数,其中pattern为要过滤的字符串 '过滤文件一行中是否有python' while True: line,file_path = yield if pattern in line: target.send(file_path) #需传递文件路径 @init def printer(): '打印文件路径' while True: file_path = yield print(file_path) g=search(opener(cat(grep(printer(),'python')))) g.send('C:\\egon')
egg_list = [] for i in range(100): egg_list.append('egg%s'% i) print(egg_list)
egg_list = ['egg%s'%i for i in range(100)] print(egg_list)
#列表内只会添加大于50的数字 egg_list = ['egg%s'%i for i in range(100) if i >50] print(egg_list)
[expression for item1 in interable if condition1 for item2 in interable if condition2 … for itemN in interable if conditionN ]
相似于express
res = [] for item1 in interable: if condition1: for item2 in interable: if condition2: … for itemN in interable: if conditionN: res.append(expression )
import os g = os.walk('C:\\egon') l = ['%s\\%s'% (i[0],j) for i in g for j in i[-1]] print(l) #['C:\\egon\\a.txt - 副本.txt', 'C:\\egon\\a.txt.txt', 'C:\\egon\\a\\a.txt.txt', 'C:\\egon\\a\\a2.txt.txt', 'C:\\egon\\b\\a.txt.txt', 'C:\\egon\\b\\a1.txt.txt']
name = 'alex' name = 'egon' res = 'sb' if name =='alex' else 'shuai' print(res) #输出shuai
(expression for item1 in interable if condition1 for item2 in interable if condition2 … for itemN in interable if conditionN )
#读取文件,并去掉每一行两头空格 f = open('a.txt') g= (line.strip() for line in f) print(next(g))
注意:因g为可迭代的,于是能够转换成列表list(g)
编程
#鸡蛋 5 3 #特斯拉 10000000.2 5 #上衣 1000 3 #裤子 2000 3 #袜子 100 #读取包含以上信息的文件,并计算总共花费 #第一种传统写法 total = [] with open('b.txt','r',encoding='utf-8') as f: for line in f: goods=line.split() #split用法及返回值需增强 # print(goods) res = float(goods[1])*float(goods[-1]) total.append(res) print(total) print(sum(total)) #第二种声明式编程写法 f=open('b.txt','r',encoding='utf-8') #不能用with 不然会IO操做报错 total=(float(line.split()[1])*float(line.split()[-1]) for line in f) print(total) print(sum(total))
#[{'name': '袜子', 'price': '100', 'num': '3'}, {'name': '袜子', 'price': '100', 'num': '3'}, {'name': '袜子', 'price': '100', 'num': '3'}, {'name': '袜子', 'price': '100', 'num': '3'}, {'name': '袜子', 'price': '100', 'num': '3'}] # 基本写法 res = [] d = {} with open('b.txt','r',encoding='utf-8') as f: for line in f: l = line.split() d['name'] = l[0] d['price'] = l[1] d['num'] = l[2] res.append(d) print(res)
with open('b.txt','r',encoding='utf-8') as f: res = (line.split() for line in f) dic_g = ({'name':line[0],'price':line[1],'num':line[2]} for line in res) goods_dic = next(dic_g) print(goods_dic['num'])
with open('b.txt') as f: d = f print(d) #有内存地址,不是很理解 print(next(d)) #报错
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">app