脚本专栏 
首页 > 脚本专栏 > 浏览文章

在python中实现对list求和及求积

(编辑:jimmy 日期: 2025/7/4 浏览:3 次 )

如下所示:

# the basic way
s = 0
for x in range(10):
 s += x

# the right way
s = sum(range(10))


# the basic way
s = 1
for x in range(1, 10):
 s *= x

# the other way
from operator import mul
reduce(mul, range(1, 10))

以上这篇在python中实现对list求和及求积就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:对python中的乘法dot和对应分量相乘multiply详解
下一篇:python 统计一个列表当中的每一个元素出现了多少次的方法