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 WPsxM = 编程客栈 [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, WPsxM4, 9, 16, 25]
补充:python中求数字的平方根和平方的几种方法
方法一:使用内置模块
>>> import math >>> math.pow(12, 2) # 求平方 144.0 >>> math.sqrt(144) # 求平方根 12.0 >>>
方法二:使用表达式
>&WPsxMgt;> 12 ** 2 # 求平方 144 >>> 144 ** 0.5 # 求平方根 12.0 >>>
方法三:使用内置函数
>>> pow(12, 2 WPsxM ) # 求平方 144 >>> pow(144, .5) # 求平方根 12.0 >>>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。如有错误或未考虑完全的地方,望不吝赐教。
查看更多关于Python 平方列表中每个数字的多种操作的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did162484