python的map()函數

來源:互聯網
上載者:User

標籤:ros   lin   int   too   mil   turn   lse   font   函數   

map() 會根據提供的函數對指定序列做映射。

第一個參數 function 以參數序列中的每一個元素調用 function 函數,返回包含每次 function 函數傳回值的新列表。

map() 函數文法:

map(function, iterable1,iterable2 ...)

function -- 函數,有兩個參數

iterable -- 一個或多個序列

1.求一個列表的平方

list1 = [1,2,3,4,5,6,7,8,9]def foo(x):    return x*xc=list(map(foo,list1))print(c)[1, 4, 9, 16, 25, 36, 49, 64, 81]
求三個列表的乘積list1 = [1,2,3,4,5,6,7,8,9]list2 = [1,2,3,4,5,6,7,8,9]list3 = [9,8,7,6,5,4,3,2,1]def fun(x,y,z):    return x*y*zd=list(map(fun,list1,list2,list3))print(d)[9, 32, 63, 96, 125, 144, 147, 128, 81]
用lambda求兩個列表之和a = [1,2,3]b = [4,5,6]c=map(lambda x,y:x + y,a,b)for i in c:    print(i,end=' ')5 7 9



python的map()函數

聯繫我們

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