python六十四课——高阶函数练习题(一)
生活随笔
收集整理的這篇文章主要介紹了
python六十四课——高阶函数练习题(一)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.lt = ['sdfasdfa', 'ewqrewrewqr', 'dsafa12312fdsafd', 'safsadf']
--> 得到長(zhǎng)度列表
2.tp = ('TOM', 'Lilei', 'JAck', ‘HanmeiMeI’)
--> 得到列表(所有元素的首字母大寫(xiě))
3.
lt1 = [1,2,3,4]
lt2 = [5,6,7,8]
計(jì)算得到:lt1[0]lt2[0] + lt1[1]lt2[1] + ...
15 + 26 +
最終得到累加以后的值 from functools import reduce lt = ['sdfasdfa', 'ewqrewrewqr', 'dsafa12312fdsafd', 'safsadf'] print(list(map(len,lt)))tp = ('TOM', 'Lilei', 'JAck','HanmeiMeI')#自定義函數(shù): def func(name):return name.capitalize()print(list(map(func,tp))) print(list(map(lambda x:x.capitalize(),tp))) print(list(map(str.capitalize,tp)))lt1=[1,2,3,4,5] lt2=[5,6,7,8]#使用map的思想得到組合數(shù)據(jù)對(duì)象(map類型),對(duì)象中的元素內(nèi)容為:15 26 37 48 lt1_map=map(lambda x:x*10,lt1) #map中的內(nèi)容為:10,20,30,40 ret_map=map(lambda x,y:x+y,lt1_map,lt2) #map中的內(nèi)容為:15,26,37,48 print(list(lt1_map)) print(list(ret_map))#使用reduce的思想將map對(duì)象中的數(shù)據(jù)進(jìn)行累加 num=reduce(lambda x,y: x + y,ret_map) print('和值為: %s' %num)
轉(zhuǎn)載于:https://www.cnblogs.com/hankleo/p/10520793.html
總結(jié)
以上是生活随笔為你收集整理的python六十四课——高阶函数练习题(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: kafka依赖zookeeper原因解析
- 下一篇: 谷歌浏览器 自动保存数据后input的黄