Skip to content

求和方法 #9

Answered by metab0t
zhaosu99 asked this question in 中文Q&A
Discussion options

You must be logged in to vote

可以看一下这里的代码

def quicksum_(expr: ExprBuilder, terms, f=None):
if isinstance(terms, dict):
iter = terms.values()
else:
iter = terms
if f:
iter = map(f, iter)
for v in iter:
expr += v
def quicksum(terms, f=None):
expr = ExprBuilder()
quicksum_(expr, terms, f)
return expr

quicksum本质上就是逐次+=

quicksum_的第一个输入变量表示在这个expr的基础上加和,而quicksum直接返回求和的结果作为一个新的ExprBuilder

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by zhaosu99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #7 on June 13, 2024 08:00.