|
15 | 15 | for 循环规则: |
16 | 16 | 操作语句 |
17 | 17 |
|
18 | | -从这个基本结构看,有着同if条件语句类似的地方:都有冒号;语句块都要缩进。是的,这是不可或缺的。 |
| 18 | +从这个基本结构看,有着同if条件语句类似的地方:都有冒号;语句块都要四个空格缩进。 |
19 | 19 |
|
20 | | -##简单的for循环例子 |
| 20 | +##从例子中理解for循环 |
21 | 21 |
|
22 | 22 | 前面介绍print语句的时候,出现了一个简单例子。重复一个类似的: |
23 | 23 |
|
|
72 | 72 | I am qiwsir |
73 | 73 | Welcome you |
74 | 74 |
|
| 75 | +以上两个例子中,是将for循环用于字符串和列表,此外,还可以用于字典、元组。例如: |
| 76 | + |
| 77 | + >>> d = dict([("website", "www.itdiffer.com"), ("lang", "python"), ("author", "laoqi")]) |
| 78 | + >>> d |
| 79 | + {'website': 'www.itdiffer.com', 'lang': 'python', 'author': 'laoqi'} |
| 80 | + >>> for k in d: |
| 81 | + print k |
| 82 | + |
| 83 | +上面的代码,输出结果是: |
| 84 | + |
| 85 | + website |
| 86 | + lang |
| 87 | + author |
| 88 | + |
| 89 | +注意到,上面的循环,其实是读取了字典的key。在字典中,有一个方法,`dict.keys()`,得到的是字典key列表。 |
| 90 | + |
| 91 | + >>> for k in d.keys(): |
| 92 | + print k |
| 93 | + |
| 94 | + website |
| 95 | + lang |
| 96 | + author |
| 97 | + |
| 98 | +这种循环方法和上面的循环方法,结果是一样的,但是,这种方法并不提倡,以为它在执行速度上表现欠佳。 |
| 99 | + |
| 100 | +如果要获得字典的value怎么办?不要忘记`dict.values()`方法。读者可以自行测试一番。 |
| 101 | + |
| 102 | +除了可以单独获得key或者value的循环之外,还可以这么做: |
| 103 | + |
| 104 | + >>> for k,v in d.items(): |
| 105 | + print k + "-->" + v |
| 106 | + |
| 107 | + website-->www.itdiffer.com |
| 108 | + lang-->python |
| 109 | + author-->laoqi |
| 110 | + |
| 111 | +在工程实践中,你一定会遇到非常大的字典。用上面的方法,要把所有的内容都读入内存,内存东西多了,可能会出麻烦。为此,Python中提供了另外的方法。 |
| 112 | + |
| 113 | + >>> for k,v in d.iteritems(): |
| 114 | + print k + "-->" + v |
| 115 | + |
| 116 | + website-->www.itdiffer.com |
| 117 | + lang-->python |
| 118 | + author-->laoqi |
| 119 | + |
| 120 | +这里是循环一个迭代器,迭代器在循环中有很多优势。除了刚才的`dict.iteritems()`之外,还有`dict.itervalues()`,`dict.iterkeys()`供你选用。 |
| 121 | + |
| 122 | + >>> d.iteritems() |
| 123 | + <dictionary-itemiterator object at 0x000000000322E368> |
| 124 | + |
| 125 | +至于对元组的循环,读者自行尝试即可。 |
| 126 | + |
| 127 | +除了上述的对象之外,for循环还能应用到哪些对象上?能不能用在数字上呢? |
| 128 | + |
| 129 | + >>> for i in 321: |
| 130 | + print i |
| 131 | + |
| 132 | + Traceback (most recent call last): |
| 133 | + File "<pyshell#48>", line 1, in <module> |
| 134 | + for i in 321: |
| 135 | + TypeError: 'int' object is not iterable |
| 136 | + |
| 137 | +报错了。这说明对于数字不能使用for循环。不过,光知道这个还不行,还要看看报错信息。 |
| 138 | + |
| 139 | +报错信息中告诉我们,‘int’对象不是可迭代的。言外之意是什么?那就是for循环所应用的对象,应该是可迭代的。那么,怎么判断一个对象是不是可迭代的呢? |
| 140 | + |
| 141 | + >>> import collections |
| 142 | + |
| 143 | +引入collections这个标准库。要判断数字321是不是可迭代的,可以这么做: |
| 144 | + |
| 145 | + >>> isinstance(321, collections.Iterable) |
| 146 | + False |
| 147 | + |
| 148 | +返回了False,说明321这个整数类型的对象,是不可迭代的。再判断一个列表对象。 |
| 149 | + |
| 150 | + >>> isinstance([1,2,3], collections.Iterable) |
| 151 | + True |
| 152 | + |
| 153 | +从返回结果,我们知道,列表[1,2,3]是可迭代的。 |
| 154 | + |
| 155 | +当然,并不是要你在使用for循环之前,非要判断某个对象是否可迭代。因为至此,你已经晓得了字符串、列表、字典、元组都是可迭代的。 |
| 156 | + |
75 | 157 | ##range(start,stop[, step]) |
76 | 158 |
|
77 | 159 | 这个内建函数,非常有必要给予说明,因为它会经常被使用。一般形式是`range(start, stop[, step])` |
|
201 | 283 | >>> range(3,100,3) |
202 | 284 | [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99] |
203 | 285 |
|
204 | | -##能够用来for的对象 |
205 | | - |
206 | | -所有的序列类型对象,都能够用for来循环。比如: |
207 | | - |
208 | | - >>> name_str = "qiwsir" |
209 | | - >>> for i in name_str: #可以对str使用for循环 |
210 | | - ... print i, |
211 | | - ... |
212 | | - q i w s i r |
213 | | - |
214 | | - >>> name_list = list(name_str) |
215 | | - >>> name_list |
216 | | - ['q', 'i', 'w', 's', 'i', 'r'] |
217 | | - >>> for i in name_list: #对list也能用 |
218 | | - ... print i, |
219 | | - ... |
220 | | - q i w s i r |
221 | | - |
222 | | - >>> name_set = set(name_str) #set还可以用 |
223 | | - >>> name_set |
224 | | - set(['q', 'i', 's', 'r', 'w']) |
225 | | - >>> for i in name_set: |
226 | | - ... print i, |
227 | | - ... |
228 | | - q i s r w |
229 | | - |
230 | | - >>> name_tuple = tuple(name_str) |
231 | | - >>> name_tuple |
232 | | - ('q', 'i', 'w', 's', 'i', 'r') |
233 | | - >>> for i in name_tuple: #tuple也能呀 |
234 | | - ... print i, |
235 | | - ... |
236 | | - q i w s i r |
237 | | - |
238 | | - >>> name_dict={"name":"qiwsir","lang":"python","website":"qiwsir.github.io"} |
239 | | - >>> for i in name_dict: #dict也不例外,这里本质上是将字典的键拿出来,成为序列后进行循环 |
240 | | - ... print i,"-->",name_dict[i] |
241 | | - ... |
242 | | - lang --> python |
243 | | - website --> qiwsir.github.io |
244 | | - name --> qiwsir |
245 | | - |
246 | | -在用for来循环读取字典键值对上,需要多说几句。 |
247 | | - |
248 | | -有这样一个字典: |
249 | | - |
250 | | - >>> a_dict = {"name":"qiwsir", "lang":"python", "email":"qiwsir@gmail.com", "website":"www.itdiffer.com"} |
251 | | - |
252 | | -曾记否?在[《字典(2)》](./117.md)中有获得字典键、值的函数:items/iteritems/keys/iterkeys/values/itervalues,通过这些函数得到的是键或者值的列表。 |
253 | | - |
254 | | - >>> for k in a_dict.keys(): |
255 | | - ... print k, a_dict[k] |
256 | | - ... |
257 | | - lang python |
258 | | - website www.itdiffer.com |
259 | | - name qiwsir |
260 | | - email qiwsir@gmail.com |
261 | | - |
262 | | -这是最常用的一种获得字典键/值对的方法,而且效率也不错。 |
263 | | - |
264 | | - >>> for k,v in a_dict.items(): |
265 | | - ... print k,v |
266 | | - ... |
267 | | - lang python |
268 | | - website www.itdiffer.com |
269 | | - name qiwsir |
270 | | - email qiwsir@gmail.com |
271 | | - |
272 | | - >>> for k,v in a_dict.iteritems(): |
273 | | - ... print k,v |
274 | | - ... |
275 | | - lang python |
276 | | - website www.itdiffer.com |
277 | | - name qiwsir |
278 | | - email qiwsir@gmail.com |
279 | | - |
280 | | -这两种方法也能够实现同样的效果,但是因为有了上面的方法,一般就少用了。但是,用也无妨,特别是第二个`iteritems()`,效率也是挺高的。 |
281 | | - |
282 | | -但是,要注意下面的方法: |
283 | | - |
284 | | - >>> for k in a_dict.keys(): |
285 | | - ... print k, a_dict[k] |
286 | | - ... |
287 | | - lang python |
288 | | - website www.itdiffer.com |
289 | | - name qiwsir |
290 | | - email qiwsir@gmail.com |
291 | | - |
292 | | -这种方法其实是不提倡的,虽然实现了同样的效果,但是效率常常是比较低的。切记。 |
293 | | - |
294 | | - >>> for v in a_dict.values(): |
295 | | - ... print v |
296 | | - ... |
297 | | - python |
298 | | - www.itdiffer.com |
299 | | - qiwsir |
300 | | - qiwsir@gmail.com |
301 | | - |
302 | | - >>> for v in a_dict.itervalues(): |
303 | | - ... print v |
304 | | - ... |
305 | | - python |
306 | | - www.itdiffer.com |
307 | | - qiwsir |
308 | | - qiwsir@gmail.com |
309 | | - |
310 | | -单独取values,推荐第二种方法。 |
311 | | - |
312 | 286 | ------ |
313 | 287 |
|
314 | 288 | [总目录](./index.md) | [上节:语句(2)](./122.md) | [下节:语句(4)](./124.md) |
|
0 commit comments