Python 平方列表中每個數字的多種操作
更新時間:2021年03月10日 10:11:49 作者:Loewi大濕
這篇文章主要介紹了Python 平方列表中每個數字的多種操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
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中求數字的平方根和平方的幾種方法
方法一:使用內置模塊
>>> 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 >>>
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
PyTorch?TensorFlow機器學習框架選擇實戰(zhàn)
這篇文章主要為大家介紹了PyTorch?TensorFlow機器學習框架選擇實戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10Python編程中NotImplementedError的使用方法
下面小編就為大家分享一篇Python編程中NotImplementedError的使用方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04