自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python的5種高級用法,效率提升沒毛??!

開發(fā) 后端
任何編程語言的高級特征通常都是通過大量的使用經(jīng)驗才發(fā)現(xiàn)的。比如你在編寫一個復(fù)雜的項目,并在 stackoverflow 上尋找某個問題的答案。然后你突然發(fā)現(xiàn)了一個非常優(yōu)雅的解決方案,它使用了你從不知道的 Python 功能!

Python的5種高級用法,效率提升沒毛?。? title=

任何編程語言的高級特征通常都是通過大量的使用經(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í)際上沒有函數(shù)名。

Python 函數(shù)通常使用 def a_function_name() 樣式來定義,但對于 lambda 函數(shù),我們根本沒為它命名。這是因為 lambda 函數(shù)的功能是執(zhí)行某種簡單的表達(dá)式或運(yùn)算,而無需完全定義函數(shù)。

lambda 函數(shù)可以使用任意數(shù)量的參數(shù),但表達(dá)式只能有一個。

  1. x = lambda a, b : a * b 
  2. print(x(5, 6)) # prints  30 
  3.  
  4. x = lambda a : a*3 + 3 
  5. print(x(3)) # prints  12 

看它多么簡單!我們執(zhí)行了一些簡單的數(shù)學(xué)運(yùn)算,而無需定義整個函數(shù)。這是 Python 的眾多特征之一,這些特征使它成為一種干凈、簡單的編程語言。

Map 函數(shù)

Map() 是一種內(nèi)置的 Python 函數(shù),它可以將函數(shù)應(yīng)用于各種數(shù)據(jù)結(jié)構(gòu)中的元素,如列表或字典。對于這種運(yùn)算來說,這是一種非常干凈而且可讀的執(zhí)行方式。

  1. def square_it_func(a): 
  2.     return a * a 
  3.  
  4. x = map(square_it_func, [1, 4, 7]) 
  5. print(x) # prints  [1, 16, 47] 
  6.  
  7. def multiplier_func(a, b): 
  8.     return a * b 
  9.  
  10. x = map(multiplier_func, [1, 4, 7], [2, 5, 8]) 
  11. print(x) # prints  [2, 20, 56] 看看上面的示例!我們可以將函數(shù)應(yīng)用于單個或多個列表。實(shí)際上,你可以使用任何 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 的元素。

詳情請看如下示例:

  1. # Our numbers 
  2. numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 
  3.  
  4. Function that filters out all numbers which are odd 
  5. def filter_odd_numbers(num): 
  6.  
  7.     if num % 2 == 0: 
  8.         return True 
  9.     else
  10.         return False 
  11.  
  12. filtered_numbers = filter(filter_odd_numbers, numbers) 
  13.  
  14. print(filtered_numbers) 
  15. # filtered_numbers = [2, 4, 6, 8, 10, 12, 14] 

我們不僅評估了每個列表元素的 True 或 False,filter() 函數(shù)還確保只返回匹配為 True 的元素。非常便于處理檢查表達(dá)式和構(gòu)建返回列表這兩步。

Itertools 模塊

Python 的 Itertools 模塊是處理迭代器的工具集合。迭代器是一種可以在 for 循環(huán)語句(包括列表、元組和字典)中使用的數(shù)據(jù)類型。

使用 Itertools 模塊中的函數(shù)讓你可以執(zhí)行很多迭代器操作,這些操作通常需要多行函數(shù)和復(fù)雜的列表理解。關(guān)于 Itertools 的神奇之處,請看以下示例:

  1. from itertools import * 
  2.  
  3. # Easy joining of two lists into a list of tuples 
  4. for i in izip([1, 2, 3], [ a ,  b ,  c ]): 
  5.     print i 
  6. # ( a , 1) 
  7. # ( b , 2) 
  8. # ( c , 3) 
  9.  
  10. # The count() function returns an interator that  
  11. # produces consecutive integers, forever. This  
  12. # one is great for adding indices next to your list  
  13. # elements for readability and convenience 
  14. for i in izip(count(1), [ Bob ,  Emily ,  Joe ]): 
  15.     print i 
  16. # (1,  Bob ) 
  17. # (2,  Emily ) 
  18. # (3,  Joe )     
  19.  
  20. # The dropwhile() function returns an iterator that returns  
  21. all the elements of the input which come after a certain  
  22. # condition becomes false for the first time.  
  23. def check_for_drop(x): 
  24.     print  Checking:  , x 
  25.     return (x > 5) 
  26.  
  27. for i in dropwhile(should_drop, [2, 4, 6, 8, 10, 12]): 
  28.     print  Result:  , i 
  29.  
  30. # Checking: 2 
  31. # Checking: 4 
  32. # Result: 6 
  33. # Result: 8 
  34. # Result: 10 
  35. # Result: 12 
  36.  
  37.  
  38. # The groupby() function is great for retrieving bunches 
  39. of iterator elements which are the same or have similar  
  40. # properties 
  41.  
  42. a = sorted([1, 2, 1, 3, 2, 1, 2, 3, 4, 5]) 
  43. for key, value in groupby(a): 
  44.     print(key, value), end=   ) 
  45.  
  46. # (1, [1, 1, 1]) 
  47. # (2, [2, 2, 2])  
  48. # (3, [3, 3])  
  49. # (4, [4])  
  50. # (5, [5])  

Generator 函數(shù)

Generator 函數(shù)是一個類似迭代器的函數(shù),即它也可以用在 for 循環(huán)語句中。這大大簡化了你的代碼,而且相比簡單的 for 循環(huán),它節(jié)省了很多內(nèi)存。

比如,我們想把 1 到 1000 的所有數(shù)字相加,以下代碼塊的第一部分向你展示了如何使用 for 循環(huán)來進(jìn)行這一計算。

如果列表很小,比如 1000 行,計算所需的內(nèi)存還行。但如果列表巨長,比如十億浮點(diǎn)數(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)建十億浮點(diǎn)數(shù),你只能一次一個地把它們存儲在內(nèi)存中!Python 2.x 中的 xrange() 函數(shù)就是使用 generator 來構(gòu)建列表。

上述例子說明:如果你想為一個很大的范圍生成列表,那么就需要使用 generator 函數(shù)。如果你的內(nèi)存有限,比如使用移動設(shè)備或邊緣計算,使用這一方法尤其重要。

也就是說,如果你想對列表進(jìn)行多次迭代,并且它足夠小,可以放進(jìn)內(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. # (1) Using a for loopv 
  2. numbers = list() 
  3.  
  4. for i in range(1000): 
  5.     numbers.append(i+1) 
  6.  
  7. total = sum(numbers) 
  8.  
  9. # (2) Using a generator 
  10.  def generate_numbers(n): 
  11.      num, numbers = 1, [] 
  12.      while num < n: 
  13.            numbers.append(num) 
  14.      num += 1 
  15.      return numbers 
  16.  total = sum(generate_numbers(1000)) 
  17.  
  18.  # (3) range() vs xrange() 
  19.  total = sum(range(1000 + 1)) 
  20.  total = sum(xrange(1000 + 1)) 

 

 

責(zé)任編輯:龐桂玉 來源: 機(jī)器學(xué)習(xí)算法與Python學(xué)習(xí)
相關(guān)推薦

2009-07-20 09:51:55

提升Windows20企業(yè)部署

2017-05-15 16:30:49

NoSQLMySQLOracle

2023-03-29 08:36:33

國產(chǎn)數(shù)據(jù)庫開源

2021-08-03 09:55:37

Python函數(shù)編程語言

2018-02-24 12:08:52

Python開發(fā)技巧

2020-06-17 08:12:05

Kubernetes容器

2024-09-30 11:36:15

2024-10-23 09:00:00

數(shù)據(jù)分析Pandas

2021-07-04 22:22:23

Webpack5前端工具

2023-12-01 15:54:44

2015-07-09 16:34:36

BYOD自帶設(shè)備

2015-07-28 10:42:34

DevOpsIT效率

2017-08-08 10:45:01

Python編程語言

2015-10-26 08:55:07

提升Python編程語言

2017-03-10 10:16:37

PythonRequests庫

2016-10-09 14:36:39

PythonWeb ServiceInstagram

2011-07-29 13:17:35

HTML 5

2012-05-21 13:57:47

數(shù)據(jù)中心電源效率

2017-12-21 15:41:11

華為云

2020-03-23 08:36:18

Python編程代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號