Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 16th, 2004, 09:23 AM
Johnny5ive Johnny5ive is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 18 Johnny5ive User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 53 m 27 sec
Reputation Power: 0
Angry WTH? What am I missing?

I just recently got back into programming with python, and am completely confounded.

What is wrong with this code? Am I missing something absolutely obvious?

Code:
word=["ten", "two", "#five", "#four", "two", "one"]
for i in word:
        print 'i equals "%s"' % i
        print word[i]
        if word[i][0] != '#':
            lastchan +=1
        elif word[i][0] == '#' and word[i+1][0] == '#':
            lastchan +=1  


Whenever I run this code, I get:

i equals "ten"
Traceback (most recent call last):
File "C:\Documents and Settings\JohnnyFive\Desktop\test.py", line 4, in ?
print word[i]
TypeError: list indices must be integers

Thie prints are there for debugging purposes. WTH am I doing wrong?

It seems that python is filling the "i" variable with the actual value of word[i], instead of the number. Is it a bug? I've checked a million different ways, including two computers and three python installs. It's gotta be something simple, but I can't figure out what!

**Edit** This doesn't really show what i'm trying to accomplish, but I figure it is rather obvious. I tore this out of a script i'm writing for Xchat that was causing me problems.

Basically I get passed a list that has several channel names on it, and I want to create a variable with the list index of the last channel name in the list. Thanks in advance!

Reply With Quote
  #2  
Old October 16th, 2004, 12:13 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Your right, in your code i is being assigned the value of word for each iteration though the list.

Generally if you wanted to iterate though a sequence by index rather than it's value you would use the range() or enumerate() functions, depending on your needs i.e.

Code:
>>> sequence = ('one', 'two', 'three', 'four', 'five')
>>> 
>>> for value in sequence: print value

one
two
three
four
five
>>> for index in range(len(sequence)): print sequence[index]

one
two
three
four
five
>>> for index, value in enumerate(sequence): print index, value

0 one
1 two
2 three
3 four
4 five
>>> for index in range(len(sequence)): print index, sequence[index]

0 one
1 two
2 three
3 four
4 five
>>>


Hopefully this will help you figure out whats going wrong with your program , and how to fix it. If you get stuck just ask.

Edit: Colours thanks to Grims Syntax program.

Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : October 16th, 2004 at 01:02 PM. Reason: Extended & coloured example

Reply With Quote
  #3  
Old October 16th, 2004, 11:43 PM
Johnny5ive Johnny5ive is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 18 Johnny5ive User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 53 m 27 sec
Reputation Power: 0
Thank you very much for your help. Looking at it now I understand that it really was me... I thought I had a bugged python or something.

And with your examples have figured out how to do what I want several ways, and it works.

Although, I have a question that I hope you can explain.

While:

Code:
sequence = ['one', 'two', 'three', 'four', 'five']
for index, value in enumerate(sequence): print index

works and prints the index correctly. The following, however, prints the index and index value in a list:
Code:
sequence = ['one', 'two', 'three', 'four', 'five']
for index in enumerate(sequence): print index
>>> 
(0, 'one')
(1, 'two')
(2, 'three')
(3, 'four')
(4, 'five')
>>> 

And I do not understand why. I assume it has something to do with the way enumerate works, and the iteration, but reading the python docs on enumerate() it isn't completely clear how it works. Thanks in advance

Reply With Quote
  #4  
Old October 17th, 2004, 07:42 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
Originally Posted by Johnny5ive
...And I do not understand why. I assume it has something to do with the way enumerate works, and the iteration, but reading the python docs on enumerate() it isn't completely clear how it works. Thanks in advance


Right again, your getting pretty good at this . This happens because enumerate returns a tuple of two vakyes (index, value) which you then unpack into the variables in the for loop . So if you have only one variable the tuple is assigned to it, if you have two variables then Python assigns each part of the tuple to those variables .

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > WTH? What am I missing?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT