Reading data from file: x(i),y(i) form

Skip Montanaro skip at pobox.com
Fri Feb 27 17:04:32 EST 2004


    Satish> x=[ ]
    Satish> y=[ ]
    ...

    Satish> I get the following error:

    Satish> x[i],y[i]=map(float,line.replace('D','E').split( ))
    Satish> Index Error: list index out of range

That's because at the point you make the assignment neither x nor y have
i'th elements.  Lists are not extended by accessing nonexistent indexes.
You should use x.append and y.append instead (I also prefer list
comprehensions over map()):

    xi,yi= [float(item) for item in line.replace('D','E').split()]
    x.append(xi)
    y.append(yi)

Skip




More information about the Python-list mailing list