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 >>>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。如有错误或未考虑完全的地方,望不吝赐教。
相关文章:
1. 一文总结Vue和React的异同2. 详解Python requests模块3. uni-app结合.NET 7实现微信小程序订阅消息推送4. H5页面使用audio标签播放音频5. js中textContent、innerText和innerHTML的用法以及区别6. 使用PHP实现实时数据可视化功能的示例详解7. Vue2 中的数据劫持简写示例8. set rs=conn.execute,set rs=server.createobject(“ADODB.recordset”)的性能对比9. Vue实现动态路由导航的示例10. ASP.NET MVC使用Log4Net记录异常日志并跳转到静态页

网公网安备