社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python-列表的平方根

guest7238 • 3 年前 • 1340 次点击  

取列表中每个数字的平方根。

对于sqrt_list函数中的此问题:

  1. 取列表中每个项目的平方根,
  2. 将每个平方项存储在另一个列表中,然后
  3. 返回此列表。

名单(11,22,33) def sqrt_列表(列表): ret=[] 对于列表中的我: ret.append(i%) 返回ret

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129660
 
1340 次点击  
文章 [ 1 ]  |  最新文章 3 年前
Freddy Mcloughlan
Reply   •   1 楼
Freddy Mcloughlan    3 年前

也许是这个?

# Use this import for all examples
from math import sqrt

old = [1,2]
new = [sqrt(x) for x in old]

函数形式

def sqrt_list(old):
    return [sqrt(x) for x in old]

def sqrt_list(old):
    new_list = []

    for i in old:
        new_list.append(sqrt(i))

    return new_list

哪里:

print(sqrt_list([11, 22, 33]))

产出:

[3.3166247903554, 4.69041575982343, 5.744562646538029]