您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:Python 平方列表中每个数字的多种操作

51自学网 2021-10-30 22:49:46
  python
这篇教程Python 平方列表中每个数字的多种操作写得很实用,希望能帮到您。

map

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 >>>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持51zixue.net。如有错误或未考虑完全的地方,望不吝赐教。


Python 使用 consul 做服务发现示例详解
tensorflow2.0实现复杂神经网络(多输入多输出nn,Resnet)
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。