欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python 平方列表中每个数字的多种操作

时间:2021-09-05 09:44:48|栏目: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
 
>>>

上一篇:python Django批量导入数据

栏    目:Python代码

下一篇:Python标准模块--ContextManager上下文管理器的具体用法

本文标题:Python 平方列表中每个数字的多种操作

本文地址:http://www.codeinn.net/misctech/172658.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有