Pepe
Pepe

Reputation: 213

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?

Upvotes: 0

Views: 702

Answers (4)

Paco Valdez
Paco Valdez

Reputation: 1975

you could initialize a list like this:

data=[]

then you could add data like this:

data.append(f)

Upvotes: 3

Samvel
Samvel

Reputation: 182

He needs "i" b/c it starts from 1 in the collection. For your code to work use:

data = {} # this is dictionary and not list

Upvotes: -1

tharen
tharen

Reputation: 1300

For plotting matplotlib is a good choice and easy to install and use.

import pylab

pylab.plot(data)
pylab.show()

Upvotes: 0

NPE
NPE

Reputation: 500167

One possibility:

data = []

while(f<50):
    f = ramp(fr,f,df)
    data.append(f)

Here, i is no longer needed.

Upvotes: 3

Related Questions