gpt4 book ai didi

python - 在 python 列表中返回多个数字

转载 作者:太空宇宙 更新时间:2023-11-04 08:58:48 25 4
gpt4 key购买 nike

n = [3, 5, 7]

def double_list(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
return x[i]

print double_list(n)

出于某种原因,这个 python 脚本在运行时只返回列表中的第一项而不是所有三项...有人可以帮我吗?!

最佳答案

使用 list comprehension 代替。

double_list = [ x*2 for x in n ]

相同的结果,短四倍,可读性高一万亿倍。

Because readability counts .

关于python - 在 python 列表中返回多个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27683312/

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