[零基礎學python]關於循環的小伎倆

不論是while仍是for,所發起的循環,在python編程中是常常被用到的。特別是for,通常認爲,它要比while快,並且也容易寫(是否容易,可能因人而異,可是,執行時間快,是的確的),所以在實踐中,for用的比較多點,不是說while就不用,好比前面所列舉而得那個猜數字遊戲,在業務邏輯上,用while就更容易理解(固然是限於那個遊戲的業務須要而言)。另外,在某些狀況下,for也不是簡單地把對象中的元素遍歷一遍,好比有有隔一個取一個的要求,等等。python

在編寫代碼的實踐中,爲了對付循環中的某些要求,須要用一些其它的函數,好比前面已經介紹過的range就是一個被看作循環中的計數器的好東西。mysql

range

《有容乃大的list(4)》中,專門對range()這個內置函數作了詳細介紹,看官能夠回到那節教程複習一番。這裏重點是複習並展現一下它的for循環中,作爲計數器的使用。git

還記得曾經在教程中有一個問題:列出100之內被3整除的數。下面引用那個問題的代碼和運行結果。github

#! /usr/bin/env python
#coding:utf-8

aliquot = []

for n in range(1,100):
    if n%3 == 0:
        aliquot.append(n)

print aliquot

代碼運行結果:sql

[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]

這個問題,若是改寫一下(也有網友在博客中提出了改寫方法)數據庫

>>> aliquot = [ x for x in range(1,100) if x%3==0 ] #用list解析,本質上跟上面無太大差別
>>> aliquot
[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]

>>> aliquot = range(3,100,3)    #這種方法更簡單。這是博客中一網友提供。
>>> aliquot
[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]

若是有一個由字母組成的字符串,只想隔一個從字符串中取一個字母。能夠這樣來實現,這是range()的一個重要用途。編程

>>> one = "Ilikepython" 
>>> new_list = [ one[i] for i in range(0,len(one),2) ]
>>> new_list
['I', 'i', 'e', 'y', 'h', 'n']

固然,間隔的舉例,是能夠任意指定的。仍是前面那個問題,還能夠經過下面的方式,選出全部可以被3整除的數。數組

>>> all_int = range(1,100)
>>> all_int
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> aliquot = [ all_int[i] for i in range(len(all_int)) if all_int[i]%3==0 ]
>>> aliquot
[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]

經過上述實例,主要是讓看官理解range()在for循環中計數器的做用。app

zip

《不可思議的for》中,已經對zip進行了介紹,此處還要提到這個函數,不只僅是複習,還能深刻一下,更主要是它也會經常被用到循環之中。函數

zip是用於並行遍歷的函數。

好比有兩個list,元素是由整數組成,若是計算對應位置元素的和。一種方法是經過循環,分別從兩個list中取出元素,而後求和。

>>> list1 = range(2,10,2)
>>> list1
[2, 4, 6, 8]
>>> list2 = range(11,20,2)
>>> list2
[11, 13, 15, 17, 19]
>>> result = [ list1[i]+list2[i] for i in range(len(list1)) ]
>>> result
[13, 17, 21, 25]

正如在《不可思議的for》中講述的那樣,上面的方法不是很完美,在上一講中有比較完美一點的代碼,請看官欣賞。

zip完成上面的任務,是這麼作的:

>>> list1
[2, 4, 6, 8]
>>> list2
[11, 13, 15, 17, 19]
>>> for a,b in zip(list1,list2):
...     print a+b, 
... 
13 17 21 25

zip()的做用就是把list1和list2兩個對象中的對應元素放到一個元組(a,b)中,而後對這兩個元素進行操做。

>>> list1
[2, 4, 6, 8]
>>> list2
[11, 13, 15, 17, 19]
>>> zip(list1,list2)
[(2, 11), (4, 13), (6, 15), (8, 17)]

對這個功能,看官能夠理解爲,將兩個list壓縮成爲(zip)一個list,只不過找不到配對的就丟掉了。

可以壓縮,也可以解壓縮,用下面的方式就是反過來了。

>>> result = zip(list1,list2)
>>> result
[(2, 11), (4, 13), (6, 15), (8, 17)]
>>> zip(*result)
[(2, 4, 6, 8), (11, 13, 15, 17)]

列位注意觀察,解壓縮獲得的結果,跟前面壓縮前的結果相比,第二項就少了一個元素19,由於在壓縮的時候就丟掉了。

這彷佛跟for沒有什麼關係呀。彆着急,思考一個問題,看看如何求解:

問題描述:有一個dictionary,myinfor = {"name":"qiwsir","site":"qiwsir.github.io","lang":"python"},將這個字典變換成:infor = {"qiwsir":"name","qiwsir.github.io":"site","python":"lang"}

解法有幾個,若是用for循環,能夠這樣作(固然,看官若是有方法,歡迎貼出來)。

>>> infor = {}
>>> for k,v in myinfor.items():
...     infor[v]=k
... 
>>> infor
{'python': 'lang', 'qiwsir.github.io': 'site', 'qiwsir': 'name'}

下面用zip()來試試:

>>> dict(zip(myinfor.values(),myinfor.keys()))
{'python': 'lang', 'qiwsir.github.io': 'site', 'qiwsir': 'name'}

嗚呼,這是什麼狀況?原來這個zip()還能這樣用。是的,本質上是這麼回事情。若是將上面這一行分解開來,看官就明白其中的奧妙了。

>>> myinfor.values()    #獲得兩個list
['python', 'qiwsir', 'qiwsir.github.io']
>>> myinfor.keys()
['lang', 'name', 'site']
>>> temp = zip(myinfor.values(),myinfor.keys())     #壓縮成一個list,每一個元素是一個tuple
>>> temp
[('python', 'lang'), ('qiwsir', 'name'), ('qiwsir.github.io', 'site')]

>>> dict(temp)                          #這是函數dict()的功能,將上述列表轉化爲dictionary
{'python': 'lang', 'qiwsir.github.io': 'site', 'qiwsir': 'name'}

至此,是否是明白zip()和循環的關係了呢?有了它可讓某些循環簡化。特別是在用python讀取數據庫的時候(好比mysql),zip()的做用更會顯現。

enumerate

enumerate的詳細解釋,在《再深點,更懂list》中已經有解釋,這裏姑且複習。

若是要對一個列表,想獲得其中每一個元素的偏移量(就是那個腳標)和對應的元素,怎麼辦呢?能夠這樣:

>>> mylist = ["qiwsir",703,"python"]
>>> new_list = []
>>> for i in range(len(mylist)):
...     new_list.append((i,mylist[i]))
... 
>>> new_list
[(0, 'qiwsir'), (1, 703), (2, 'python')]

enumerate的做用就是簡化上述操做:

>>> enumerate(mylist)
<enumerate object at 0xb74a63c4>    #出現這個結果,用list就能顯示內容.相似的會在後面課程出現,意味着可迭代。
>>> list(enumerate(mylist))
[(0, 'qiwsir'), (1, 703), (2, 'python')]

對enumerate()的深入闡述,還得看這個官方文檔:

class enumerate(object)
| enumerate(iterable[, start]) -> iterator for index, value of iterable
|
| Return an enumerate object. iterable must be another object that supports
| iteration. The enumerate object yields pairs containing a count (from
| start, which defaults to zero) and a value yielded by the iterable argument.
| enumerate is useful for obtaining an indexed list:
| (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
|
| Methods defined here:
|
| getattribute(...)
| x.getattribute('name') <==> x.name
|
| iter(...)
| x.iter() <==> iter(x)
|
| next(...)
| x.next() -> the next value, or raise StopIteration

Data and other attributes defined here:
new =
T.new(S, ...) -> a new object with type S, a subtype of T

對官方文檔,有的朋友可能看起來有點迷糊,沒關係,至少瀏覽一下,看個大概。由於隨着我的實踐的愈來愈多,對文檔的含義理解會愈來愈深入。這就比如令狐沖,剛剛學習了獨孤九劍的口訣和招式後,理解不是很深入,只有在不斷的打打殺殺實踐中,特別跟東方不敗等高手過招以後,才能愈來愈體會到獨孤九劍中的奧妙。

相關文章
相關標籤/搜索