SyntaxError: can't assign to function call (For Loop)

匿名 (未验证) 提交于 2019-12-03 01:41:02

问题:

Hi I am trying to create a new array from a previous array. such that in new array the first element is mean of first 20 elements from existing array. Here is my code. I am not sure why its not working.

#Averages RPMA=[] for i in range(9580):   for j in range (0,191600):     a=RPM.iloc[j:j+20]     RPMA(i)= a.mean() 

回答1:

Looks to me like you're using the wrong kind of brackets. This line:

RPMA(i)= a.mean() 

...should probably be this:

RPMA[i]= a.mean() 

But I'm no Python expert. I guessing that it thinks RPMA(i) is a function because you use parentheses, in which case you would be trying to assign a value to a function call, like the error says.

However trying to assign a new value past the end of the array will result in an IndexError because the array element doesn't exist, and will need to be added. What you can do instead is this:

RPMA.append(a.mean()) 

...which will append the data to the end of the array (i.e. adding a new element).



回答2:

Thanks everyone. As suggested by most of you, I made below changes in code and now its working just fine!

RPMA=[] for j in range (0,191600, 20):     a=RPM.iloc[j:j+19]     RPMA.append(a.mean()) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!