[Tutor] Python List Help
Dave Angel
davea at ieee.org
Mon Oct 19 19:35:21 CEST 2009
Mike Sweany wrote:
> Hi all,
>
>
>
> I am a PHP developer that just started learning Python for a specific
> application and the transition has been pretty easily assisted by google,
> but I just don’t see the issue with this one?
>
>
>
> I’ve got a list that created and populate in a loop that shows the correct
> info when I print it, but if I try to print array[0], it says the list index
> is out of range.
>
>
>
> Here is the code snippet. Basically, I have a link here that I am pulling
> the href info from and splitting it up, only keeping the 5th value, which I
> am then adding to the playerid array.
>
>
>
> playerid = []
>
> for i in range(0, players):
>
> player = playerlink[i]['href']
>
> breakup = player.split('/')
>
> playerid.append(breakup[4])
>
>
>
> stats = test.findAll(text=True)
>
> print len(playerid) ß this shows the expected result
>
> print playerid[0] ßthis kills the script
>
>
>
>
>
>
> 33 print len(playerid)
>
>
> 34 print playerid[0]
>
>
> 35
>
>
>
> playerid = []
>
> <type 'exceptions.IndexError'>: list index out of range
> args = ('list index out of range',)
> message = 'list index out of range'
>
> If I change the print playerid[0] to print playerid[1], same error, but if I
> change it to print playerid[2] or higher, it will show the same error, but
> the playerid= [] will show the actual list values in the debug.
>
> Any help would be appreciated, thank you!
>
>
>
>
I'm very confused about your formatting. You seem to be using tabs with
a large column setting (prefer 4 columns, and tabs should become spaces
in the editor). But there's no context. So this stuff is indented, but
it's not inside a function?? And the numbers 33, 34, and 35, what are
they from?
What debugger are you running, that somehow displays list values when
you assign an empty list to playerid? That makes no sense. If it's an
exotic debugger, then perhaps you should be doing this straight from the
python interpreter.
I suggest that until you're comfortable enough with python to know what
to include, that you post exactly what happened, without trying so hard
to customize it.
Show the file, in its entirety (if it's too big, then you would need a
smaller example). And put markers at begin and end so we can see what
part is the file.
Then if you're running from the interpreter prompt, show the whole
session, from import xxxx to the traceback error.
Note that if you want to print variables from the imported program,
you'd use
>>> print mymodule.playerid
The following is not intended to be good programming, it's intended to
encapsulate what you already had, with some initialization to avoid
needing other stuff that's presumably irrelevant here.
***file stuff2.py ****
#!/usr/bin/env python
#-*- coding: utf-8 -*-
link0 = {"href":"this /is /a /test/of the stuff/before here"}
link1 = {"href":"this /is /a /test/different stuff/before here"}
link2 = {"href":"this /is /a /test/other stuff/before here"}
playerlink = [link0, link1, link2]
players = len(playerlink)
def doit():
global playerid
playerid = []
for i in range(0, players):
player = playerlink[i]['href']
breakup = player.split('/')
playerid.append(breakup[4])
print len(playerid) # this shows the expected result
print playerid[0] #this gets an exception
if __name__ == "__main__":
doit()
*** end file *****
And now the interpreter session:
M:\Programming\Python\sources\dummy>python26
ActivePython 2.6.2.2 (ActiveState Software Inc.) based on
Python 2.6.2 (r262:71600, Apr 21 2009, 15:05:37) [MSC v.1500 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import stuff2
>>> stuff2.doit()
3
of the stuff
>>> print len(stuff2.playerid)
3
>>> print stuff2.playerid[0]
of the stuff
>>>
Of course, I didn't get any error. But if you do, your transcript should
be enough information for people to better tell why.
One more thing: copy/paste, don't retype. Otherwise you may obscure the
problem or introduce others.
DaveA
More information about the Tutor
mailing list