#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    18
    Rep 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!
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    Last edited by netytan; October 16th, 2004 at 01:02 PM. Reason: Extended & coloured example
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    18
    Rep 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
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo