Python 平方列表中每個數(shù)字的多種操作
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 >>>
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解3. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析4. 淺談python出錯時traceback的解讀5. Python importlib動態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼6. android studio 打包自動生成版本號與日期,apk輸入路徑詳解7. 利用promise及參數(shù)解構(gòu)封裝ajax請求的方法8. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解9. Nginx+php配置文件及原理解析10. vue使用webSocket更新實(shí)時天氣的方法
