Python無聊的總結

來源:互聯網
上載者:User

標籤:get   lambda   name   style   過程   ace   com   ast   ted   

 

在公司無聊的時候看了前輩寫的python代碼,突然發現一個比較好玩的python運算式:

 1 lambda x,y:x+y 

咋一看,這個應該類似方法之類的,上網查了查,所以特此總結下

lambda:上代碼

冒號前邊的代表參數
冒號後邊的代表運算式
傳回值類型跟參數有關

 1 >>> lambda x:x 2 <function <lambda> at 0x01B50730>#如果不作傳遞的話就是這樣 3 >>> 4 >>> 5 >>> la=lambda x:x+y 6 >>> la(2) 7 Traceback (most recent call last): 8   File "<stdin>", line 1, in <module> 9   File "<stdin>", line 1, in <lambda>10 NameError: global name ‘y‘ is not defined11 >>>12 >>>13 >>> la=lambda x,y:x+y14 >>>15 >>>16 >>> la(1,2)17 3

 

**傳回值:

1 >>> la(1,range(5))#列表2 [0, 1, 2, 3, 4]3 >>>4 >>>5 >>> la(1,(1,2))#元組6 (1, 2)7 >>> la(1,‘xyz‘)#字串8 ‘xyz‘

 

**從這裡可以看得出來,lambda還是挺便捷的。如果是複雜的表達適合,個人覺的還是不要使用lambda

filter:

第一個參數:方法
第二個參數:序列
對序列中的每一個元素用方法迭代病產生一個列表

1 >>> def fil(x):2 ...     return x % 2 != 0 and x % 3 != 03 ...4 >>>5 >>>6 >>> filter(fil,range(1,100))7 [1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53, 55, 59, 618 , 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97]9 >>>

 

**如果第一個參數傳入的是None的話,則只遍曆序列裡邊的值,比如:

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

 

reduce

第一個參數:方法名
第二個參數:序列
第三個參數:備用的,不傳的話為0
實在不明白為什麼取這個名字,請忽略我的抱怨,上代碼看看
傳回值跟參數有關

 1 >>> def add(x,y): 2 ...     print ‘x=‘ + str(x), ‘y=‘ + str(y) 3 ...     return x + y 4 ... 5 >>> reduce(add,range(10)) 6 x=0 y=1#第3個參數不傳的時候,可以看出,x的值是給定的值,之後就是疊加後的值 7 x=1 y=2 8 x=3 y=3 9 x=6 y=410 x=10 y=511 x=15 y=612 x=21 y=713 x=28 y=814 x=36 y=915 45

 

**帶第三個參數的時候

 1 >>> reduce(add,range(10),20) 2 x=20 y=0#從這裡就可以看出,reduce是從第三個參數給定值然後迭代的 3 x=20 y=1 4 x=21 y=2 5 x=23 y=3 6 x=26 y=4 7 x=30 y=5 8 x=35 y=6 9 x=41 y=710 x=48 y=811 x=56 y=912 6513 >>> reduce(add,‘xyz‘)14 x=x y=y15 x=xy y=z16 ‘xyz‘17 >>> reduce(add,‘xyz‘,10)18 x=10 y=x19 Traceback (most recent call last):20   File "<stdin>", line 1, in <module>21   File "<stdin>", line 3, in add22 TypeError: unsupported operand type(s) for +: ‘int‘ and ‘str‘23 >>>24 >>>25 >>> reduce(add,‘xyz‘,‘10‘)26 x=10 y=x27 x=10x y=y28 x=10xy y=z29 ‘10xyz‘30 >>>

 

map

第一個參數:方法名
第二個參數:序列,也可以是多個序列(前提是方法必須支援多個參數)
傳回值是個列表

 1 >>> map(add,range(10))#從這裡可以看出和reduce讀取參數的方式還是不同的。其實也差不多,因為reduce第三個參數有初始化 2 Traceback (most recent call last): 3   File "<stdin>", line 1, in <module> 4 TypeError: add() takes exactly 2 arguments (1 given) 5 >>> 6 >>> 7 >>> map(add,range(10),range(10)) 8 x=0 y=0 9 x=1 y=110 x=2 y=211 x=3 y=312 x=4 y=413 x=5 y=514 x=6 y=615 x=7 y=716 x=8 y=817 x=9 y=918 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]19 >>>20 >>> map(ret,‘x‘)21 x=x22 [‘x‘]23 >>>24 >>>25 >>> map(ret,(1,2))26 x=127 x=228 [1, 2]29 >>>30 >>>31 >>> map(ret,[1,2])32 x=133 x=234 [1, 2]35 >>>

 

如果map中的方法只有一個參數,則只能傳入一個序列;如果有多個,則傳入多個序列。再對每個序列中的值迭代,這個就比較奇怪了,也不知在編碼過程中有什麼用處

總的來說,我在python的編碼過程中很少用到這幾個方法,估計面試的時候會用到

generated by haroopad

Python無聊的總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.