[Tutor] Retrieving numbers from a text
alan.gauld at btinternet.com
Wed Oct 24 01:11:40 CEST 2012
On 23/10/12 23:45, Rufino Beniga wrote:
First, you realize I hope that we don;t do homework for you but we will
try to help steer you the right way...
So the simple answer is...
> tell me i can't do it, then i'll prove you wrong!
OK, You can't do it.
Is that enough?
I thought not, read on...
> *_The previous problem that I have already done:_*
> Iterate a chaotic one-dimensional map: Write a program
> OneDMapIterate.py that iterates the function f(x) = 4.0 x ( 1 - x ).
> First, set the initial condition x = 0.3. Then apply f(x) repeatedly to
> each new x. Iterate for 100 steps, printing on each line the iteration
> number and the successive x values to file OneDMap.txt.
> My program:
> #Writes contents of loop to file
> print >> file, 'Iterate number ', i, ': ', x
Note you are writing more than you were asked to to the file.
This slightly complicates the second problem... but only slightly.
> My problem is, I don't know how to get ONLY the numbers from the
> OneDMap.txt file. Also, assuming that I am able to do just that, how
> would I get them into an array?
One thing at a time.
Can you read a line from the file as a string?
Can you split a string into its constituent parts? In this case they
'Iterate number '
Can you extract the 2nd and 4th items (do you see why writing
more than you were asked complicates it?)
Can you convert strings to numbers, specifically integers and floats?
Now the second question:
Can you create a new empty list?
Can you append items to that list?
Putting them together:
Can you write a loop that
-reads a line from the file
-extracts the data you want
-appends it to your list
If you can do the above you have pretty much 'proved me wrong'...
If not come back with more specific questions.
Author of the Learn to Program web site
More information about the Tutor