日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Python的三個重要函數(shù)詳解_python

作者:第十六月夜 ? 更新時間: 2022-03-31 編程語言

一、遍歷函數(shù)(map)

map函數(shù)用于遍歷序列,對序列中每個元素進行操作,最終獲取新的序列。

lis=[2,3,4,5]
new_list=map(lambda x:x+10,lis)
list(new_list)

a=[7,8,9,10]
b=[10,15,20,25]
new_ab=map(lambda x,y:x+y,a,b)
list(new_ab)

二、篩選函數(shù)(filter)

filter函數(shù)用于對序列中的元素進行篩選,最終獲取符合條件的序列

new_list=filter(lambda x:x>4,lis)
list(new_list)

三、累計函數(shù)(reduce)

reduce函數(shù)用于對序列內(nèi)所有元素進行累計操作。

reduce函數(shù)在functools模塊中,先進行導入

from functools import reduce

reduce的第1個參數(shù)是至少含有有兩個參數(shù)的函數(shù),第2個參數(shù)是將要循環(huán)的序列,第3個參數(shù)是初始值?

a=[7,8,9,10]
reduce(lambda arg1,arg2:arg1+arg2,a)

?也可以先給定一個初始值再累加

reduce(lambda arg1,arg2:arg1+arg2,a,20)

總結

原文鏈接:https://blog.csdn.net/KK_1657654189/article/details/122475493

欄目分類
最近更新