本章要点
- 迭代器的创建及其使用
- 列表的解析
- 装饰器的应用
迭代的概念:
- 迭代在数学上的概念:
是依据一个公式在前一个值得基础上推到下一个值如此反复得到自己想要的值为止
比如著名的牛顿迭代公式 - python中什么是可迭代的对象?
所谓可迭代的对象就是python中的一个序列如果在迭代工具的作用下一次可以返回
一个结果或者是一个支持iter的一个对象 - 什么是迭代协议?
所谓迭代协议就是python中对象中包含有一个名为next的方法,这个方法可以
使得在得到前一个结果之后进入下一个结果 - 迭代器的概念
python中的迭代器从数学上理解就相当于一个数学公式一个函数,使用一次可以产生一个值,并且
在前一个值得基础上得到下一个值,在python中所谓迭代器就是一个满足迭代协议的迭代对象。
注意:(可迭代的未必是迭代器如range对象他本身不是自身的迭代器,要想变成得到迭代器可以使用iter方法获得
可迭代的对象与迭代器的差别就在与一个可以不能直接使用迭代协议,需要使用方法生成迭代器后使用。另
外一个可以直接掉用next方法.)
# 先从使用简单的迭代工具遍历序列开始
for x in 'abcde': #迭代字符串
print(x,end=' ')
print('\n')
for x in [1,2,3,4,5]: #迭代列表
print((x+10),end=' ')
print('\n')
for x in (2,4,6,8,10): #迭代元组
print(x,end=' ')
print('\n')
for x in {'a':7,'b':8,'c':9}: #迭代字典
print(x,end=' ')
a b c d e
11 12 13 14 15
2 4 6 8 10
b c a
#文件迭代器
f = open('myfile.txt') # 打开文件
f.readlines() # 读取第一行
'hello text file\n'
f.readline() # 第二行
'goodbye text file\n'
f.readline() # 文件末尾
''
f = open('myfile.txt')
f.readlines()
['hello text file\n', 'goodbye text file\n']
'''
文件有一个方法__next__
文件本身就是一个迭代器
下面用迭代协议next读文件
当读到末尾时迭代协议会引发
StopIteration异常,即终止读
文件,而不是返回空字符。
'''
F = open('myfile.txt')
F.__next__() #读第一行
'hello text file\n'
F.__next__() #读第二行
'goodbye text file\n'
F.__next__() #文件末尾引发异常
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-7-d969fa8435cc> in <module>()
----> 1 F.__next__() #文件末尾引发异常
StopIteration:
# 使用迭代工具读取文件
for line in open('myfile.txt'):
print(line,end = ' ')
hello text file
goodbye text file
# 手动迭代iter和next的使用
f = open('myfile.txt')
f.__next__()
'hello text file\n'
f.__next__()
'goodbye text file\n'
f = open('myfile.txt')
next(f)
'hello text file\n'
next(f)
'goodbye text file\n'
L = [1,2,3] #列表为可迭代的
a = iter(L) # 返回L的迭代器对象
next(a) # 使用内置 next()函数
1
a.__next__() #调用迭代器方法
2
next(a)
3
next(L) #显然列表自身不是迭代器,不包含迭代协议
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-30-f699c34d7040> in <module>()
----> 1 next(L)
TypeError: 'list' object is not an iterator
# 使用迭代工具
for x in L:
print(x,end = ' ')
1 2 3
D = {'a':1,'b':2,'c':3}
next(D) # D不是构造器
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-32-3469fae33c45> in <module>()
5 '''
6 D = {'a':1,'b':2,'c':3}
----> 7 next(D)
TypeError: 'dict' object is not an iterator
iterd = iter(D) # 获取iter构造器
next(iterd) # 此构造器为获取字典键值得构造器
'b'
'''
在使用迭代工具迭代的时候
实际上隐含了调用iter将可迭代的
对象变成迭代器,如下因为把对象D变成了
迭代器,从而for遍历的是字典的键
'''
for x in D:
print(x,end=' ')
b c a
# 列表解析
'''
列表解析基本语法:
L = [ f(x) for x in t]
含义为讲对象t中的每一个元素实行f变换后
添加为一个新列表的一个元素
'''
a =[1,2,3,4]
L = [x+10 for x in a]
print(L)
[11, 12, 13, 14]
'''
实际上每一个列表解析都可以用多个for
语句代替,下式子代替上式子
'''
c = []
for x in a:
c.append(x+10)
print(c)
[11, 12, 13, 14]
#在文件上使用列表解析
f = open('myfile.txt')
line = f.readlines()
line
['hello text file\n', 'goodbye text file\n']
# 上式等价下式
line = [line.rstrip() for line in open('myfile.txt')]
line
['hello text file', 'goodbye text file']
'''
扩展的列表解析语法:
在语句的最后加if条件语句来挑选符合条件的对象,允许有多个for..in..if语句
'''
line = [line.rstrip() for line in open('myfile.txt') if line[0]=='h']
line
['hello text file']
# 连接列表解析
L = [x+y for x in 'abc' for y in 'cba']
L
['ac', 'ab', 'aa', 'bc', 'bb', 'ba', 'cc', 'cb', 'ca']
# range迭代器
#使用内置函数range后返回一个可迭代对象
R = range(10)
R
range(0, 10)
next(R) # 显然range不是自身的构造器
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-53-da893503ae54> in <module>()
----> 1 next(R) # 显然range不是自身的构造器
TypeError: 'range' object is not an iterator
for x in R:
print(x,end=' ')
0 1 2 3 4 5 6 7 8 9
'''
map,zip,filter迭代器
这三个与range的不同之处在于
这三个内置函数返回的对象都是
自身的迭代器
'''
a = ['aaa','bbb','ccc']
M = map(len,a)
M
<map at 0x22861e28e48>
next(M) #
3
a = zip(['a','b','c'],('a','b','c'))
a
<zip at 0x22861e00e08>
next(a)
('a', 'a')
list(a)
[('b', 'b'), ('c', 'c')]
总结:
可迭代对象与迭代器在很大程度上节约了内存,它避免了一次将序列所有的值都列出来,可以根据需求列出自己想要的值。另外迭代器与可迭代对象的使用使得代码更简洁的同时也提高了代码的运行效率。
2018.10.30
网友评论