File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5656
5757以上的循环举例中,显示了对str的字符依次获取,也涉及了list,感觉不过瘾呀。那好,看下面对list的循环:
5858
59- >>> ls_line
60- ['Hello', 'I am qiwsir', 'Welcome you', '']
59+ >>> ls_line = ['Hello', 'I am qiwsir', 'Welcome you', '']
60+
6161 >>> for word in ls_line:
6262 ... print word
6363 ...
110110
111111在工程实践中,你一定会遇到非常大的字典。用上面的方法,要把所有的内容都读入内存,内存东西多了,可能会出麻烦。为此,Python中提供了另外的方法。
112112
113- >>> for k,v in d.iteritems():
113+ >>> for k,v in d.iteritems(): #注意:仅在Python2中可用。
114114 print k + "-->" + v
115115
116116 website-->www.itdiffer.com
117117 lang-->python
118118 author-->laoqi
119119
120- 这里是循环一个迭代器,迭代器在循环中有很多优势。除了刚才的` dict.iteritems() ` 之外,还有` dict.itervalues() ` ,` dict.iterkeys() ` 供你选用。
120+ 这里是循环一个迭代器,迭代器在循环中有很多优势。除了刚才的` dict.iteritems() ` 之外,还有` dict.itervalues() ` ,` dict.iterkeys() ` 供你选用(以上三个iter-()都只用在Python2中,Python3中没有。) 。
121121
122- >>> d.iteritems()
122+ >>> d.iteritems() #注意:仅在Python2中可用。
123123 <dictionary-itemiterator object at 0x000000000322E368>
124124
125125至于对元组的循环,读者自行尝试即可。
287287
288288[ 总目录] ( ./index.md )   ;  ;  ; |  ;  ;  ; [ 上节:语句(2)] ( ./122.md )   ;  ;  ; |  ;  ;  ; [ 下节:语句(4)] ( ./124.md )
289289
290- 如果你认为有必要打赏我,请通过支付宝:** qiwsir@126.com ** ,不胜感激。
290+ 如果你认为有必要打赏我,请通过支付宝:** qiwsir@126.com ** ,不胜感激。
You can’t perform that action at this time.
0 commit comments