成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

Python 平方列表中每個(gè)數(shù)字的多種操作

瀏覽:6日期:2022-06-25 15:40:30

Python 平方列表中每個(gè)數(shù)字的多種操作

map

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ò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Python 編程
相關(guān)文章: