【python】强大而有用的工具内置迭代工具 itertools - accumulate【叠加,叠乘等快速 *** 作】(3)

【python】强大而有用的工具内置迭代工具 itertools - accumulate【叠加,叠乘等快速 *** 作】(3),第1张

项目场景:

需求:快速实现一个列表数据的叠加,跌乘等 *** 作。


问题描述

代码如下

# coding=utf-8
# 作者:Administrator
# 创建时间:2022 2022/5/9 9:32
# IDE:PyCharm
# 描述:多个可迭代对象构建成一个新的可迭代对象
import operator
from itertools import accumulate

# exp1 :
list1 = [1, 2, 3, 4, 3]

print(list(accumulate(list1)))  # 默认是叠加的 *** 作[1, 3, 6, 10, 13]
print(list(accumulate(list1, min)))  # 返回当前项的最小值 [1, 1, 1, 1, 1]
print(list(accumulate(list1, max)))  # 返回当前项的最大值[1, 2, 3, 4, 4]
print(list(accumulate(list1, operator.mul)))  # 叠乘的 *** 作[1, 2, 6, 24, 72]  , *** 作的内容就是[1,1*2,1*2*3,1*2*3*4,1*2*3*4*3]
print(list(accumulate(list1, operator.add)))  # 叠加
print(operator.mul(2000000000000000000000000,300000000000000000000000))# 计算两个数值的乘积 600000000000000000000000000000000000000000000000


解决方案:

强大而有用的工具内置迭代工具 itertools - accumulate【叠加,叠乘等快速 *** 作】,可以让你的代码简单,可读性强

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/905341.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-15
下一篇2022-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存