gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Python 平方列表中每个数字的多种操作由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

map

map(function,iterable) 。

  1. x = [1,2,3,4,5]
  2. def square(num):
  3. return num*num
  4. print(list(map(square,x)))
  5. #output:[1, 4, 9, 16, 25]

lambda

lambda x

  1. x = [1,2,3,4,5]
  2. print(list(map(lambda num:num*num, x)))
  3. #output:[1, 4, 9, 16, 25]

list comprehensions

[funtion for item in iterable] 。

  1. print([ num*num for num in [1,2,3,4,5]])
  2. #output:[1, 4, 9, 16, 25]

补充:Python中求数字的平方根和平方的几种方法 。

方法一:使用内置模块

  1. >>> import math
  2.  
  3. >>> math.pow(12, 2) # 求平方
  4. 144.0
  5.  
  6. >>> math.sqrt(144) # 求平方根
  7. 12.0
  8.  
  9. >>>

方法二:使用表达式

  1. >>> 12 ** 2 # 求平方
  2. 144
  3.  
  4. >>> 144 ** 0.5 # 求平方根
  5. 12.0
  6.  
  7. >>>

方法三:使用内置函数

  1. >>> pow(12, 2) # 求平方
  2. 144
  3.  
  4. >>> pow(144, .5) # 求平方根
  5. 12.0
  6.  
  7. >>>

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

原文链接:https://blog.csdn.net/weixin_42317507/article/details/84190576 。

最后此篇关于Python 平方列表中每个数字的多种操作的文章就讲到这里了,如果你想了解更多关于Python 平方列表中每个数字的多种操作的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com