如何在python中求和 [英] How to do a sum in python

查看:29
本文介绍了如何在python中求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在没有像 这样的循环的情况下在 python 中表示一个总和这里

I was wondering how one can represent a sum in python without loops like here

我们在哪里:

def rosen(x):
    """The Rosenbrock function"""
    return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)

我的函数如下:V(theta) = Sum(i=1->N)[a0*(cos(i*theta)]

预先感谢您的帮助:):)

Thank you in advance for your help :):)

推荐答案

类似于:

def V(theta,N):
    return sum(a0*(cos(i*theta) for i in range(1,N+1))
print V(theta,N) 

或者你可以使用lambda:

V =lambda theta,N : sum(a0*(cos(i*theta) for i in range(1,N+1))   
print V(theta,N) 

这篇关于如何在python中求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆