迭代器:
1.序列
>>> atuple=[1,2,3,4]
>>> i=iter(atuple)
>>> while True:
try:
j=i.next()
print j
except StopIteration:
break
2.字典
>>> legends={('Poe','author'):(1809,1849,1976),
('Gaudi','architect'):(1852,1906,1987),
('Freud','psychoamalyst'):(1856,1939,1990)}
>>> for eachLegend in legends:
print 'Name:%s\t Occupation:%s' %eachLegend
print 'Birth:%s\t Deatch:%s\t Album:%s\n' %legends[eachLegend]
3.文件
for line in myFile:或for line in myFile.readlines()
4.在可变对象的迭代中,不要修改对象的值
创建迭代器:iter(obj)将检查obj是否是序列
一个实现了__iter__()和next()方法的类就可以创建迭代器
5.列表解析,生成的就是可迭代的列表
[expr for iter_var in iterable]
如:>>> [x*2 for x in range(6)]
[0, 2, 4, 6, 8, 10]
map(func,iter()):对列表成员运用一个操作
map(lambda x:x*2,range(6))等效于[x*2 for x in range(6)]
可见列表解析是可以取代map和lambda的,而且效率更高
升级加过滤条件:[expr for iter_var in iterable if cond_expr]
奇数:[ x for x in seq if x%2]
文件中单词计数:len([word for line in f for word in line.split()])
([word for line in f for word in line.split()])将返回单词列表,len返回列表长度即单词个数
文件非空白字符的个数:sum([len(word) for line in f for word in line.split()])
生成器:
迭代器是一次生成所有数据,用来创建整个列表,生成器每次只创建一个条目,然后暂停代码执行,稍后恢复,对于处理大数据使用生成器更能优化内存。
1.生成器格式:
列表解析:
[expr for iter_var in iterable if cond_expr]
生成器表达式:
(expr for iter_var in iterable if cond_expr)
2.文件
sum(len(word) for line in f for word in line.split())
用生成器代替迭代器会节省内存,提高执行效率
3.交叉配对
rows=[1,2,3,17]
def cols():
yield 56
yield 2
yield 1
pair_product=((i,j) for i in rows for j in cols())
for pair in pair_product:
print pair
3.遍历文件中的每行,找出最长的行
---------------------------------------------------------------------------------
方法一:
第一种写法无法区分文件中的空行和文件结束,因为readline都将返回控制符传
---------------------------------------------------------------------------------
coding:utf-8
def longestLine():
f=open('F:\\traces.txt','r')
longest=0
while True:
linelen=len(f.readline().strip()) #去除结尾的换行符
if not linelen:
break
if linelen>longest:
longest=linelen
f.close()
return longest
if __name__=="__main__":
print longestLine()
-----------------------------------------------------------------------------------
方法二:readlines()读取所有行的列表
----------------------------------------------------------------------------------
#coding:utf-8
def longestLine():
f=open('F:\\write.txt','r')
longest=0
alline=f.readlines()
f.close()#可以很快释放f对象给其他对象使用,但是对于大数据一次读取所有内容会占用较大内存
for line in alline:
linelen=len(line.strip())
if linelen>longest:
longest=linelen
return longest
if __name__=='__main__':
print longestLine()
-------------------------------------------------------------
方法三:使用迭代器
-------------------------------------------------------------
#coding:utf-8
def longestLine():
f=open('F:\\write.txt','r')
longest=0
allLines=[x.strip() for x in f.readlines()]
f.close()
for line in allLines:
linelen=len(line)
if linelen>longest:
longest=linelen
return longest
if __name__=='__main__':
print longestLine()
--------------------------------------------------------------
方法四:迭代器优化代码
------------------------------------------------------------
#coding:utf-8
def longestLine():
f=open('F:\\write.txt','r')
longline=max([len(x.strip()) for x in f.readlines()])
f.close()
return longline
if __name__=='__main__':
print longestLine()
---------------------------------------------------------------------
方法五:使用生成器
-----------------------------------------------------------------------
#coding:utf-8
def longestLine():
f=open('F:\\write.txt','r')
longline=max((len(x.strip()) for x in f.readlines()))
f.close()
return longline
if __name__=='__main__':
print longestLine()
网友评论