map(function,iterable)
x = [1,2,3,4,5] def square(num): return num*num print(list(map(square,x))) #output:[1, 4, 9, 16, 25]
lambda x:
x = [1,2,3,4,5] print(list(map(lambda num:num*num, x))) #output:[1, 4, 9, 16, 25]
[funtion for item in iterable]
print([ num*num for num in [1,2,3,4,5]]) #output:[1, 4, 9, 16, 25]
補充:Python中求數(shù)字的平方根和平方的幾種方法
>>> import math >>> math.pow(12, 2) # 求平方 144.0 >>> math.sqrt(144) # 求平方根 12.0 >>>
>>> 12 ** 2 # 求平方 144 >>> 144 ** 0.5 # 求平方根 12.0 >>>
>>> pow(12, 2) # 求平方 144 >>> pow(144, .5) # 求平方根 12.0 >>>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
標簽:昆明 西寧 浙江 文山 梅州 石家莊 懷化 錫林郭勒盟
巨人網(wǎng)絡(luò)通訊聲明:本文標題《Python 平方列表中每個數(shù)字的多種操作》,本文關(guān)鍵詞 Python,平方,列,表中,每個,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。