[Tutor] list index out of range
Steven D'Aprano
steve at pearwood.info
Mon Sep 13 00:56:55 CEST 2010
On Fri, 10 Sep 2010 07:52:20 am Todd Ballard wrote:
> I am attempting to have a cummalative total of the y values and
> receive a "list index out of range" error message
How unfortunate.
Do you have an actual question to ask, or are you just sharing?
If you are having problems fixing the error, what does the stack
traceback say? Not just the error message, which is the least important
part of the error, but the entire traceback. This will show *where* the
function occurs, as well as *what* the error is, and sometimes *why* as
well.
A couple of other comments follow:
> import filereader
> from filereader import *
It's rare to actually need to do both of this together. Are you sure you
need both? It's generally better to stick to the first form, and have
fully-qualified names like filereader.read_array. If that's too long,
it's best to import only the functions you actually need, which in your
case seems to be:
from filereader import read_array
The "import *" form should be considered advanced usage best avoided
unless you know what you're doing.
> My_Path="C:\\Python26\\assignment2\\datadownload.txt"
A little-known fact: Windows supports both \ and / as the directory
separator. Since Python uses backslash for the escape character, it
gets tedious and error-prone to write strings with lots of backslashes.
So the best way to write pathnames under Windows in Python is with
forward-slashes:
My_Path="C:/Python26/assignment2/datadownload.txt"
> y=[]
> for i in xrange(0,365):
There's no need to say xrange(0, 365). Just xrange(365) will have the
same effect, because the starting index defaults to 0.
> y+=[daily_solar_radiation["MJ"][i]]
It's generally more efficient to use:
y.append(daily_solar_radiation["MJ"][i])
instead of y += [...] as you do, although for only 365 items it won't
make a huge difference.
> from filereader import *
You've already done this above, you don't need to do this again.
--
Steven D'Aprano
More information about the Tutor
mailing list