鮮為人知的 Python 五種高級特征
任何編程語言的高級特征通常都是通過大量的使用經(jīng)驗才發(fā)現(xiàn)的。比如你在編寫一個復(fù)雜的項目,并在 stackoverflow 上尋找某個問題的答案。然后你突然發(fā)現(xiàn)了一個非常優(yōu)雅的解決方案,它使用了你從不知道的 Python 功能!
這種學(xué)習(xí)方式太有趣了:通過探索,偶然發(fā)現(xiàn)什么。
下面是 Python 的 5 種高級特征,以及它們的用法。
Lambda 函數(shù)
Lambda 函數(shù)是一種比較小的匿名函數(shù)——匿名是指它實際上沒有函數(shù)名。
Python 函數(shù)通常使用 def a_function_name() 樣式來定義,但對于 lambda 函數(shù),我們根本沒為它命名。這是因為 lambda 函數(shù)的功能是執(zhí)行某種簡單的表達式或運算,而無需完全定義函數(shù)。
lambda 函數(shù)可以使用任意數(shù)量的參數(shù),但表達式只能有一個。
- x = lambda a, b : a * b
- print(x(5, 6)) # prints 30
- x = lambda a : a*3 + 3
- print(x(3)) # prints 12
看它多么簡單!我們執(zhí)行了一些簡單的數(shù)學(xué)運算,而無需定義整個函數(shù)。這是 Python 的眾多特征之一,這些特征使它成為一種干凈、簡單的編程語言。
Map 函數(shù)
Map() 是一種內(nèi)置的 Python 函數(shù),它可以將函數(shù)應(yīng)用于各種數(shù)據(jù)結(jié)構(gòu)中的元素,如列表或字典。對于這種運算來說,這是一種非常干凈而且可讀的執(zhí)行方式。
- def square_it_func(a):
- return a * a
- x = map(square_it_func, [1, 4, 7])
- print(x) # prints [1, 16, 47]
- def multiplier_func(a, b):
- return a * b
- x = map(multiplier_func, [1, 4, 7], [2, 5, 8])
- print(x) # prints [2, 20, 56] 看看上面的示例!我們可以將函數(shù)應(yīng)用于單個或多個列表。實際上,你可以使用任何 Python 函數(shù)作為 map 函數(shù)的輸入,只要它與你正在操作的序列元素是兼容的。
Filter 函數(shù)
filter 內(nèi)置函數(shù)與 map 函數(shù)非常相似,它也將函數(shù)應(yīng)用于序列結(jié)構(gòu)(列表、元組、字典)。二者的關(guān)鍵區(qū)別在于 filter() 將只返回應(yīng)用函數(shù)返回 True 的元素。
詳情請看如下示例:
- # Our numbers
- numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
- # Function that filters out all numbers which are odd
- def filter_odd_numbers(num):
- if num % 2 == 0:
- return True
- else:
- return False
- filterfiltered_numbers = filter(filter_odd_numbers, numbers)
- print(filtered_numbers)
- # filtered_numbers = [2, 4, 6, 8, 10, 12, 14]
我們不僅評估了每個列表元素的 True 或 False,filter() 函數(shù)還確保只返回匹配為 True 的元素。非常便于處理檢查表達式和構(gòu)建返回列表這兩步。
Itertools 模塊
Python 的 Itertools 模塊是處理迭代器的工具集合。迭代器是一種可以在 for 循環(huán)語句(包括列表、元組和字典)中使用的數(shù)據(jù)類型。
使用 Itertools 模塊中的函數(shù)讓你可以執(zhí)行很多迭代器操作,這些操作通常需要多行函數(shù)和復(fù)雜的列表理解。關(guān)于 Itertools 的神奇之處,請看以下示例:
- from itertools import *
- # Easy joining of two lists into a list of tuples
- for i in izip([1, 2, 3], [ a , b , c ]):
- print i
- # ( a , 1)
- # ( b , 2)
- # ( c , 3)
- # The count() function returns an interator that
- # produces consecutive integers, forever. This
- # one is great for adding indices next to your list
- # elements for readability and convenience
- for i in izip(count(1), [ Bob , Emily , Joe ]):
- print i
- # (1, Bob )
- # (2, Emily )
- # (3, Joe )
- # The dropwhile() function returns an iterator that returns
- # all the elements of the input which come after a certain
- # condition becomes false for the first time.
- def check_for_drop(x):
- print Checking: , x
- return (x > 5)
- for i in dropwhile(should_drop, [2, 4, 6, 8, 10, 12]):
- print Result: , i
- # Checking: 2
- # Checking: 4
- # Result: 6
- # Result: 8
- # Result: 10
- # Result: 12
- # The groupby() function is great for retrieving bunches
- # of iterator elements which are the same or have similar
- # properties
- a = sorted([1, 2, 1, 3, 2, 1, 2, 3, 4, 5])
- for key, value in groupby(a):
- print(key, value), end= )
- # (1, [1, 1, 1])
- # (2, [2, 2, 2])
- # (3, [3, 3])
- # (4, [4])
- # (5, [5])
Generator 函數(shù)
Generator 函數(shù)是一個類似迭代器的函數(shù),即它也可以用在 for 循環(huán)語句中。這大大簡化了你的代碼,而且相比簡單的 for 循環(huán),它節(jié)省了很多內(nèi)存。
比如,我們想把 1 到 1000 的所有數(shù)字相加,以下代碼塊的第一部分向你展示了如何使用 for 循環(huán)來進行這一計算。
如果列表很小,比如 1000 行,計算所需的內(nèi)存還行。但如果列表巨長,比如十億浮點數(shù),這樣做就會出現(xiàn)問題了。使用這種 for 循環(huán),內(nèi)存中將出現(xiàn)大量列表,但不是每個人都有無限的 RAM 來存儲這么多東西的。Python 中的 range() 函數(shù)也是這么干的,它在內(nèi)存中構(gòu)建列表。
代碼中第二部分展示了使用 Python generator 函數(shù)對數(shù)字列表求和。generator 函數(shù)創(chuàng)建元素,并只在必要時將其存儲在內(nèi)存中,即一次一個。這意味著,如果你要創(chuàng)建十億浮點數(shù),你只能一次一個地把它們存儲在內(nèi)存中!Python 2.x 中的 xrange() 函數(shù)就是使用 generator 來構(gòu)建列表。
上述例子說明:如果你想為一個很大的范圍生成列表,那么就需要使用 generator 函數(shù)。如果你的內(nèi)存有限,比如使用移動設(shè)備或邊緣計算,使用這一方法尤其重要。
也就是說,如果你想對列表進行多次迭代,并且它足夠小,可以放進內(nèi)存,那最好使用 for 循環(huán)或 Python 2.x 中的 range 函數(shù)。因為 generator 函數(shù)和 xrange 函數(shù)將會在你每次訪問它們時生成新的列表值,而 Python 2.x range 函數(shù)是靜態(tài)的列表,而且整數(shù)已經(jīng)置于內(nèi)存中,以便快速訪問。
- # (1) Using a for loopv
- numbers = list()
- for i in range(1000):
- numbers.append(i+1)
- total = sum(numbers)
- # (2) Using a generator
- def generate_numbers(n):
- num, numbers = 1, []
- while num < n:
- numbers.append(num)
- num += 1
- return numbers
- total = sum(generate_numbers(1000))
- # (3) range() vs xrange()
- total = sum(range(1000 + 1))
- total = sum(xrange(1000 + 1))