[英]Python iteration with array
I guess it is a simple question, I am doing simple while iteration and want to save data within data array so I can simple plot it. 我想这是一个简单的问题,我在迭代时做的很简单,想在数据数组中保存数据,以便可以简单地绘制它。
tr = 25 #sec
fr = 50 #Hz
dt = 0.002 #2ms
df = fr*(dt/tr)
i=0;
f = 0
data = 0
while(f<50):
i=i+1
f = ramp(fr,f,df)
data[i] = f
plot(data)
How to correctly define data array? 如何正确定义数据数组? How to save results in array?
如何将结果保存在数组中?
you could initialize a list like this: 您可以像这样初始化一个列表:
data=[]
then you could add data like this: 那么您可以像这样添加数据:
data.append(f)
One possibility: 一种可能性:
data = []
while(f<50):
f = ramp(fr,f,df)
data.append(f)
Here, i
is no longer needed. 在这里,
i
不再需要。
For plotting matplotlib is a good choice and easy to install and use. 对于绘图, matplotlib是一个不错的选择,并且易于安装和使用。
import pylab
pylab.plot(data)
pylab.show()
He needs "i" b/c it starts from 1 in the collection. 他需要“ i” b / c,它从集合中的1开始。 For your code to work use:
为了使您的代码正常工作,请使用:
data = {} # this is dictionary and not list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.