Python 平方列表中每個數字的多種操作
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]
補充: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 >>>
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章:
