On Thu, 27 Jan 2005 00:02:45 -0700, Steven Bethard <[EMAIL PROTECTED]> wrote: > Stephen Thorne wrote: > > f = file('input', 'r') > > labels = f.readline() # consume the first line of the file. > > > > Easy Option: > > for line in f.readlines(): > > x, y = line.split() > > x = float(x) > > y = float(y) > > > > Or, more concisely: > > for line in f.readlines(): > > x, y = map(float, line.split()) > > Somewhat more memory efficient: > > lines_iter = iter(file('input')) > labels = lines_iter.next() > for line in lines_iter: > x, y = [float(f) for f in line.split()] > > By using the iterator instead of readlines, I read only one line from > the file into memory at once, instead of all of them. This may or may > not matter depending on the size of your files, but using iterators is > generally more scalable, though of course it's not always possible.
I just did a teensy test. All three options used exactly the same amount of total memory. I did all I did in the name of clarity, considering the OP was on his first day with python. How I would actually write it would be: inputfile = file('input','r') inputfile.readline() data = [map(float, line.split()) for line in inputfile] Notice how you don't have to call iter() on it, you can treat it as an iterable to begin with. Stephen. -- http://mail.python.org/mailman/listinfo/python-list