Python-列表的平方根

客人7238

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

对于 sqrt_list 函数中的这个问题:

  1. 取列表中每个项目的平方根,
  2. 将每个平方项存储在另一个列表中,并且
  3. 返回此列表。
alist = [11,22,33]
def sqrt_list(alist):
    ret = []
    for i in alist:
        ret.append(i %)
    return ret
弗雷迪麦克劳伦

也许这个?

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章