Python 平方列表中每個(gè)數(shù)字的多種操作
map(function,iterable)
x = [1,2,3,4,5]def square(num): return num*numprint(list(map(square,x)))#output:[1, 4, 9, 16, 25]lambda
lambda x:
x = [1,2,3,4,5]print(list(map(lambda num:num*num, x)))#output:[1, 4, 9, 16, 25]list comprehensions
[funtion for item in iterable]
print([ num*num for num in [1,2,3,4,5]])#output:[1, 4, 9, 16, 25]
補(bǔ)充:Python中求數(shù)字的平方根和平方的幾種方法
方法一:使用內(nèi)置模塊>>> import math >>> math.pow(12, 2) # 求平方144.0 >>> math.sqrt(144) # 求平方根12.0 >>>方法二:使用表達(dá)式
>>> 12 ** 2 # 求平方144 >>> 144 ** 0.5 # 求平方根12.0 >>> 方法三:使用內(nèi)置函數(shù)
>>> pow(12, 2) # 求平方144 >>> pow(144, .5) # 求平方根12.0 >>>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法2. Python TestSuite生成測(cè)試報(bào)告過程解析3. Python OpenCV去除字母后面的雜線操作4. docker /var/lib/docker/aufs/mnt 目錄清理方法5. JAMon(Java Application Monitor)備忘記6. IntelliJ IDEA設(shè)置背景圖片的方法步驟7. Spring security 自定義過濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)8. Python os庫常用操作代碼匯總9. Java類加載機(jī)制實(shí)現(xiàn)步驟解析10. 增大python字體的方法步驟
