python 內建函數 map filter reduce

來源:互聯網
上載者:User

標籤:包含   font   import   filter   str   port   列表   序列   div   

map(函數名,可遍曆迭代的對象)
# 列組元素全加 10# map(需要做什麼的函數,遍曆迭代對象)函數    map()遍曆序列得到一個列表,列表的序號和個數和原來一樣l = [2,3,4,5,6,7,8]t = list(map(lambda x:x+10,l))     #遍曆 l,l 裡的元素全加10  map得到的結果是可迭代對象所以要listprint(t)                    #===>[12, 13, 14, 15, 16, 17, 18]

 

filter(函數名,可遍曆迭代的對象)
# filter(返回Ture的函數,遍曆迭代對象)函數     filter()  遍曆序列保留返回Ture的元素組成一個列表   利用filter過濾l = [1,2,3,4,5]l1 = list(filter(lambda x:x>2,l))        #遍曆列表 l,返回 l 裡大於2的元素   filter,返回可迭代對象所以要listprint(l1)                  #===>[3, 4, 5]

 

reduce(函數名,可遍曆迭代的對象)
from functools import reduce          #用reduce,需要包含這個架構l = [2,5,8,10]print(reduce(lambda x,y:x+y,l))       #===>25      #reduce用來計算print(reduce(lambda x,y:x+y,l,5))     #===>30     #預設初始值為None,改為5

 

 
 


python 內建函數 map filter reduce

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.