我想通过采取这些参数除了启动:
lambda x: summation(lambda x: x, 1, lambda x: x + 1, x)
替代这些变量回到原来的功能,简化IT:
def inner_function(x):
soma = 0
linf = 1
while linf <= x:
soma += linf + 1
linf += 1
return soma
简化多了几分:
def inner_function(x):
soma = 0
for linf in range(1, x + 1):
soma += linf
return soma
还有一点点:
inner_function = lambda x: sum(range(1, x + 1))
而且一些:
inner_function = lambda x: x * (x + 1)/2
现在你原来的功能就变成了:
def summation(calc_termo, linf, prox, lsup):
soma = 0
while linf <= lsup:
soma = soma + calc_termo(linf)
linf = prox(linf)
return soma
summation(inner_function, 1, lambda x: x + 1, 5)
或者:
def summation(linf, prox, lsup):
soma = 0
while linf <= lsup:
soma = soma + linf * (linf + 1)/2
linf = prox(linf)
return soma
summation(1, lambda x: x + 1, 5)
你可以把它从那里。我:
summation = lambda: sum(n * (n + 1)/2 for n in range(6))
等于:
sum(sum(range(n + 1)) for n in range(6))
谢谢你的帮助,这真的很有帮助我现在可以更好地理解它 –