[Tutor] Python List Help

Mike Sweany mike at fozzil.net
Tue Oct 20 04:46:53 CEST 2009


Thanks for the reply and Tips Dave... 

I think the formatting came out weird because I didn’t specify plain text
when I created the email.

I'm using Google App Engine and it's internal debugger shows what I posted
earlier when the code breaks, I agree that what I am seeing from the
debugger makes no sense...

Thanks for the tips, I will look at what you have and see if I can make it
work...

>From top to bottom, I am using BeautifulSoup to parse an html document and
what you are seeing is a part of the processing that just didn’t jive with
practices that I had already carried over and had working with the rest of
the code. I don’t have it set up to do the import with an interpreter
session like you laid out below, basically I am printing the stuff as it
goes, it's in a loop and this code is processed multiple times.

Honestly, I need to do some reading on how python works as I was plugging
along on this script with just a couple of hours into python for this
scraping project, but what you have shown me here will help and I will come
back to the list after I have a better understanding of what I am doing.

Thanks,

Mike

-----Original Message-----
From: Dave Angel [mailto:davea at ieee.org] 
Sent: Monday, October 19, 2009 1:35 PM
To: Mike Sweany
Cc: tutor at python.org
Subject: Re: [Tutor] Python List Help

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