|
| 1 | +>你竟任着刚硬不悔改的心,为自己积蓄忿怒,以致神震怒,显他公义审判的日子来到。他必照各人的行为报应各人。凡恒心行善,寻求荣耀、尊贵和不能朽坏之福的,就以永生报应他们;惟有结党不顺从真理,反顺从不义的,就以忿怒、恼恨报应他们。(ROMANS 2:7-8) |
| 2 | +
|
| 3 | +#迭代 |
| 4 | + |
| 5 | +跟一些比较牛X的程序员交流,经常听到他们嘴里冒出一个不标准的英文单词,而loop、iterate、traversal和recursion如果不在其内,总觉得他还不够牛X。当让,真正牛X的绝对不会这么说的,他们只是说“循环、迭代、遍历、递归”,然后再问“这个你懂吗?”。哦,这就是真正牛X的程序员。不过,他也仅仅是牛X罢了,还不是大神。大神程序员是什么样儿呢?他是扫地僧,大隐隐于市。 |
| 6 | + |
| 7 | +先搞清楚这些名词再说别的: |
| 8 | + |
| 9 | +- 循环(loop),指的是在满足条件的情况下,重复执行同一段代码。比如,while语句。 |
| 10 | +- 迭代(iterate),指的是按照某种顺序逐个访问列表中的每一项。比如,for语句。 |
| 11 | +- 递归(recursion),指的是一个函数不断调用自身的行为。比如,以编程方式输出著名的斐波纳契数列。 |
| 12 | +- 遍历(traversal),指的是按照一定的规则访问树形结构中的每个节点,而且每个节点都只访问一次。 |
| 13 | + |
| 14 | +对于这四个听起来高深莫测的词汇,其实前面,已经涉及到了一个——循环(loop),本节主要介绍一下迭代(iterate),看官在网上google,就会发现,对于迭代和循环、递归之间的比较的文章不少,分别从不同角度将它们进行了对比。这里暂不比较,先搞明白python中的迭代。 |
| 15 | + |
| 16 | +当然,迭代的话题如果要说起来,会很长,本着循序渐进的原则,这里介绍比较初级的。 |
| 17 | + |
| 18 | +##逐个访问 |
| 19 | + |
| 20 | +在python中,访问对象中每个元素,可以这么做:(例如一个list) |
| 21 | + |
| 22 | + >>> lst |
| 23 | + ['q', 'i', 'w', 's', 'i', 'r'] |
| 24 | + >>> for i in lst: |
| 25 | + ... print i, |
| 26 | + ... |
| 27 | + q i w s i r |
| 28 | + |
| 29 | +除了这种方法,还可以这样: |
| 30 | + |
| 31 | + >>> lst_iter = iter(lst) #对原来的list实施了一个iter() |
| 32 | + >>> lst_iter.next() #要不厌其烦地一个一个手动访问 |
| 33 | + 'q' |
| 34 | + >>> lst_iter.next() |
| 35 | + 'i' |
| 36 | + >>> lst_iter.next() |
| 37 | + 'w' |
| 38 | + >>> lst_iter.next() |
| 39 | + 's' |
| 40 | + >>> lst_iter.next() |
| 41 | + 'i' |
| 42 | + >>> lst_iter.next() |
| 43 | + 'r' |
| 44 | + >>> lst_iter.next() |
| 45 | + Traceback (most recent call last): |
| 46 | + File "<stdin>", line 1, in <module> |
| 47 | + StopIteration |
| 48 | + |
| 49 | +`iter()`是一个内建函数,其含义是: |
| 50 | + |
| 51 | +上面的`next()`就是要获得下一个元素,但是做为一名优秀的程序员,最佳品质就是“懒惰”,当然不能这样一个一个地敲啦,于是就: |
| 52 | + |
| 53 | + >>> while True: |
| 54 | + ... print lst_iter.next() |
| 55 | + ... |
| 56 | + Traceback (most recent call last): #居然报错,而且错误跟前面一样?什么原因 |
| 57 | + File "<stdin>", line 2, in <module> |
| 58 | + StopIteration |
| 59 | + |
| 60 | +先不管错误,再来一遍。 |
| 61 | + |
| 62 | + >>> lst_iter = iter(lst) #上面的错误暂且搁置,回头在研究 |
| 63 | + >>> while True: |
| 64 | + ... print lst_iter.next() |
| 65 | + ... |
| 66 | + q #果然自动化地读取了 |
| 67 | + i |
| 68 | + w |
| 69 | + s |
| 70 | + i |
| 71 | + r |
| 72 | + Traceback (most recent call last): #读取到最后一个之后,报错,停止循环 |
| 73 | + File "<stdin>", line 2, in <module> |
| 74 | + StopIteration |
| 75 | + |
| 76 | +首先了解一下上面用到的那个内置函数:iter(),官方文档中有这样一段话描述之: |
| 77 | + |
| 78 | +> iter(o[, sentinel]) |
| 79 | +
|
| 80 | +> Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, o must be a collection object which supports the iteration protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then o must be a callable object. The iterator created in this case will call o with no arguments for each call to its next() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned. |
| 81 | +
|
| 82 | +大意是说...(此处故意省略若干字,因为我相信看此文章的看官英语水平是达到看文档的水平了,如果没有,也不用着急,找个词典什么的帮助一下。) |
| 83 | + |
| 84 | +尽管不翻译了,但是还要提炼一下主要的东西: |
| 85 | + |
| 86 | +- 返回值是一个迭代器对象 |
| 87 | +- 参数需要是一个符合迭代协议的对象或者是一个序列对象 |
| 88 | +- next()配合与之使用 |
| 89 | + |
| 90 | +什么是“可迭代的对象”呢?在前面学习的时候,曾经提到过,如果忘记了请往前翻阅。 |
| 91 | + |
| 92 | +一般,我们常常将哪些能够用诸如循环语句之类的方法来一个一个读取元素的对象,就称之为可迭代的对象。那么用来循环的如for就被称之为迭代工具。 |
| 93 | + |
| 94 | +用严格点的语言说:所谓迭代工具,就是能够按照一定顺序扫描迭代对象的每个元素(按照从左到右的顺序)。 |
| 95 | + |
| 96 | +显然,除了for之外,还有别的可以称作迭代工具。 |
| 97 | + |
| 98 | +那么,刚才介绍的iter()的功能呢?它与next()配合使用,也是实现上述迭代工具的作用。 |
| 99 | + |
| 100 | +在python中,甚至在其它的语言中,迭代这块的说法比较乱,主要是名词乱,刚才我们说,那些能够实现迭代的东西,称之为迭代工具,就是这些迭代工具,不少程序员都喜欢叫做迭代器。当然,这都是汉语翻译,英语就是iterator。 |
| 101 | + |
| 102 | +看官看上面的所有例子会发现,如果用for来迭代,当到末尾的时候,就自动结束了,不会报错。如果用iter()...next()迭代,当最后一个完成之后,它不会自动结束,还要向下继续,但是后面没有元素了,于是就报一个称之为StopIteration的错误(这个错误的名字叫做:停止迭代,这哪里是报错,分明是警告)。 |
| 103 | + |
| 104 | +看官还要关注iter()...next()迭代的一个特点。当迭代对象lst_iter被迭代结束,即每个元素都读取了一遍之后,指针就移动到了最后一个元素的后面。如果再访问,指针并没有自动返回到首位置,而是仍然停留在末位置,所以报StopIteration,想要再开始,需要重新载入迭代对象。所以,当我在上面重新进行迭代对象赋值之后,又可以继续了。这在for等类型的迭代工具中是没有的。 |
| 105 | + |
| 106 | +##文件迭代器 |
| 107 | + |
| 108 | +现在有一个文件,名称:208.txt,其内容如下: |
| 109 | + |
| 110 | + Learn python with qiwsir. |
| 111 | + There is free python course. |
| 112 | + The website is: |
| 113 | + http://qiwsir.github.io |
| 114 | + Its language is Chinese. |
| 115 | + |
| 116 | +用迭代器来操作这个文件,我们在前面讲述文件有关知识的时候已经做过了,无非就是: |
| 117 | + |
| 118 | + >>> f = open("208.txt") |
| 119 | + >>> f.readline() #读第一行 |
| 120 | + 'Learn python with qiwsir.\n' |
| 121 | + >>> f.readline() #读第二行 |
| 122 | + 'There is free python course.\n' |
| 123 | + >>> f.readline() #读第三行 |
| 124 | + 'The website is:\n' |
| 125 | + >>> f.readline() #读第四行 |
| 126 | + 'http://qiwsir.github.io\n' |
| 127 | + >>> f.readline() #读第五行,也就是这真在读完最后一行之后,到了此行的后面 |
| 128 | + 'Its language is Chinese.\n' |
| 129 | + >>> f.readline() #无内容了,但是不报错,返回空。 |
| 130 | + '' |
| 131 | + |
| 132 | +以上演示的是用readline()一行一行地读。当然,在实际操作中,我们是绝对不能这样做的,一定要让它自动进行,比较常用的方法是: |
| 133 | + |
| 134 | + >>> for line in f: #这个操作是紧接着上面的操作进行的,请看官主要观察 |
| 135 | + ... print line, #没有打印出任何东西 |
| 136 | + ... |
| 137 | + |
| 138 | +这段代码之所没有打印出东西来,是因为经过前面的迭代,指针已经移到了最后了。这就是迭代的一个特点,要小心指针的位置。 |
| 139 | + |
| 140 | + >>> f = open("208.txt") #从头再来 |
| 141 | + >>> for line in f: |
| 142 | + ... print line, |
| 143 | + ... |
| 144 | + Learn python with qiwsir. |
| 145 | + There is free python course. |
| 146 | + The website is: |
| 147 | + http://qiwsir.github.io |
| 148 | + Its language is Chinese. |
| 149 | + |
| 150 | +这种方法是读取文件常用的。另外一个readlines()也可以。但是,需要有一些小心的地方,看官如果想不起来小心什么,可以在将关于文件的课程复习一边。 |
| 151 | + |
| 152 | +上面过程用next()也能够读取。 |
| 153 | + |
| 154 | + >>> f = open("208.txt") |
| 155 | + >>> f.next() |
| 156 | + 'Learn python with qiwsir.\n' |
| 157 | + >>> f.next() |
| 158 | + 'There is free python course.\n' |
| 159 | + >>> f.next() |
| 160 | + 'The website is:\n' |
| 161 | + >>> f.next() |
| 162 | + 'http://qiwsir.github.io\n' |
| 163 | + >>> f.next() |
| 164 | + 'Its language is Chinese.\n' |
| 165 | + >>> f.next() |
| 166 | + Traceback (most recent call last): |
| 167 | + File "<stdin>", line 1, in <module> |
| 168 | + StopIteration |
| 169 | + |
| 170 | +如果用next(),就可以直接读取每行的内容。这说明文件是天然的可迭代对象,不需要用iter()转换了。 |
| 171 | + |
| 172 | +再有,我们用for来实现迭代,在本质上,就是自动调用next(),只不过这个工作,已经让for偷偷地替我们干了,到这里,列位是不是应该给for取另外一个名字:它叫雷锋。 |
| 173 | + |
| 174 | +还有,列表解析也能够做为迭代工具,在研究列表的时候,看官想必已经清楚了。那么对文件,是否可以用?试一试: |
| 175 | + |
| 176 | + >>> [ line for line in open('208.txt') ] |
| 177 | + ['Learn python with qiwsir.\n', 'There is free python course.\n', 'The website is:\n', 'http://qiwsir.github.io\n', 'Its language is Chinese.\n'] |
| 178 | + |
| 179 | +至此,看官难道还不为列表解析所折服吗?真的很强大,又强又大呀。 |
| 180 | + |
| 181 | +其实,迭代器远远不止上述这么简单,下面我们随便列举一些,在python中还可以这样得到迭代对象中的元素。 |
| 182 | + |
| 183 | + >>> list(open('208.txt')) |
| 184 | + ['Learn python with qiwsir.\n', 'There is free python course.\n', 'The website is:\n', 'http://qiwsir.github.io\n', 'Its language is Chinese.\n'] |
| 185 | + |
| 186 | + >>> tuple(open('208.txt')) |
| 187 | + ('Learn python with qiwsir.\n', 'There is free python course.\n', 'The website is:\n', 'http://qiwsir.github.io\n', 'Its language is Chinese.\n') |
| 188 | + |
| 189 | + >>> "$$$".join(open('208.txt')) |
| 190 | + 'Learn python with qiwsir.\n$$$There is free python course.\n$$$The website is:\n$$$http://qiwsir.github.io\n$$$Its language is Chinese.\n' |
| 191 | + |
| 192 | + >>> a,b,c,d,e = open("208.txt") |
| 193 | + >>> a |
| 194 | + 'Learn python with qiwsir.\n' |
| 195 | + >>> b |
| 196 | + 'There is free python course.\n' |
| 197 | + >>> c |
| 198 | + 'The website is:\n' |
| 199 | + >>> d |
| 200 | + 'http://qiwsir.github.io\n' |
| 201 | + >>> e |
| 202 | + 'Its language is Chinese.\n' |
| 203 | + |
| 204 | +上述方式,在编程实践中不一定用得上,只是向看官展示一下,并且看官要明白,可以这么做,不是非要这么做。 |
| 205 | + |
| 206 | +补充一下,字典也可以迭代,看官自己不妨摸索一下(其实前面已经用for迭代过了,这次请摸索一下用iter()...next()手动一步一步迭代)。 |
| 207 | + |
| 208 | +------ |
| 209 | + |
| 210 | +[总目录](./index.md) | [上节:文件(2)](./127.md) | [下节:练习](./129.md) |
| 211 | + |
| 212 | +如果你认为有必要打赏我,请通过支付宝:**qiwsir@126.com**,不胜感激。 |
0 commit comments