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

作者:Loewi大湿 时间:2023-11-14 03:53:00 

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

来源:https://blog.csdn.net/weixin_42317507/article/details/84190576

标签:Python,平方,列表,数字
0
投稿

猜你喜欢

  • SqlServer参数化查询之where in和like实现详解

    2012-05-22 18:10:50
  • MySQL开启慢查询日志功能的方法

    2024-01-19 10:09:37
  • python 正则式使用心得

    2021-09-17 14:39:49
  • windows下python 3.9 Numpy scipy和matlabplot的安装教程详解

    2021-12-20 13:20:47
  • python实现selenium网络爬虫的方法小结

    2023-01-13 03:43:38
  • django实现支付宝支付实例讲解

    2023-08-27 04:45:44
  • Django 忘记管理员或忘记管理员密码 重设登录密码的方法

    2021-02-14 09:40:46
  • PHP 解决utf-8和gb2312编码转换问题

    2024-04-29 13:56:45
  • 利用Python进行数据可视化的实例代码

    2023-11-24 10:21:45
  • 用Python写一个无界面的2048小游戏

    2022-02-12 11:18:23
  • python中的annotate函数使用

    2021-04-10 01:52:59
  • PyQt5 关于Qt Designer的初步应用和打包过程详解

    2023-07-31 20:26:32
  • Python操作Word批量生成合同的实现示例

    2023-06-17 09:59:14
  • 浅析MySQL内存的使用说明(全局缓存+线程缓存)

    2024-01-26 02:46:20
  • Python实现批量读取图片并存入mongodb数据库的方法示例

    2021-03-25 01:51:53
  • vue实现input输入模糊查询的三种方式

    2024-05-08 10:42:12
  • Oracle以逗号分隔的字符串拆分为多行数据实例详解

    2024-01-13 16:32:46
  • Mysql中的find_in_set的使用方法介绍

    2024-01-14 06:24:19
  • 如何将数据库里的记录生成一个Excel文件?

    2009-12-03 20:09:00
  • python flask搭建web应用教程

    2023-05-14 14:16:17
  • asp之家 网络编程 m.aspxhome.com