时间:2021-09-05 09:44:48 | 栏目:Python代码 | 点击:次

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 x:
x = [1,2,3,4,5] print(list(map(lambda num:num*num, x))) #output:[1, 4, 9, 16, 25]
[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 >>>