![](/d/20211017/6e7967035621da6a71c96d743d5ee535.gif)
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]
補充:Python中求數(shù)字的平方根和平方的幾種方法
方法一:使用內置模塊
>>> import math
>>> math.pow(12, 2) # 求平方
144.0
>>> math.sqrt(144) # 求平方根
12.0
>>>
方法二:使用表達式
>>> 12 ** 2 # 求平方
144
>>> 144 ** 0.5 # 求平方根
12.0
>>>
方法三:使用內置函數(shù)
>>> pow(12, 2) # 求平方
144
>>> pow(144, .5) # 求平方根
12.0
>>>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- Python3 完全平方數(shù)案例
- Python 實現(xiàn)把列表中的偶數(shù)變成他的平方
- Python用二分法求平方根的案例
- python 計算平均平方誤差(MSE)的實例
- python判斷完全平方數(shù)的方法
- Python編程實現(xiàn)二分法和牛頓迭代法求平方根代碼
- Python編程之求數(shù)字平方的實例