我是新來的python編碼 和我正在寫一個代碼來計算一系列的總和,我開始通過編寫一個函數,其中輸入是總和的迭代,但是當我編譯它給了我在def G(i)
線下的錯誤:只有長度爲1的陣列可以轉換到Python標量Python錯誤只有長度爲1的數組可以轉換爲Python標量
你能幫助我,請
import matplotlib.pyplot as plt
import numpy as np
import scipy.special as sp
import pylab as pylab
def G(i):
return (sum(((-1*(2*l+1))/(4*np.pi*(l**2+l)))*sp.legendre(l)(0.5) for l in i))
pylab.ylim([-1,1])
sumrange = np.arange(1,70,1)
plt.plot(sumrange,G(sumrange),color='red')
可能的重複[TypeError:只能將長度爲1的數組轉換爲Python標量,同時嘗試按指數擬合數據](http://stackoverflow.com/questions/21687581/typeerror-only-length-1-arrays-可以轉換爲python-scalars-while-trying) –
簡短版本:使用'np.sum'而不是'sum'。 –
@aryamccarthy np.sum不起作用,問題是與範圍輸入我認爲 – outlawoutlawz