欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年03月10日 10:11:49   作者:Loewi大濕  
這篇文章主要介紹了Python 平方列表中每個數(shù)字的多種操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

map

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

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
 
>>>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評論