Python Forum

Full Version: Can't understand
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm going through this book on python 2.7 and I have to create some nosetests for these. So I have been commenting them, but I can't fully understand what the skip function does. So I gave it a list of tuples
skip([('noun', 'bear'), ('noun', 'cow'), ('noun', 'sheep'), ('noun', 'princess'), ('noun', 'boy')], 'noun')

and it only prints out the even tuples to the console. I can't seem to figure out why it does this.
Here is a snippet of my code.

def peek(word_list):
    """gets the word type in the first tuple of the list"""
    if word_list:
        # sets word to the first tuple
        word = word_list[0]
        # returns item at word's zero index
        return word[0]
    else:
        return None


def match(word_list, expecting):
    """removes the specified word type"""
    if word_list:
        # pops off the first tuple in the list
        word = word_list.pop(0)

        # if item in word's index zero is equal to expecting
        if word[0] == expecting:
            # returns list without first tuple
            return word
        else:
            return None
    else:
        return None


def skip(word_list, word_type):
    """skips specifed word type in a row
       until it reaches a new word type"""

    while peek(word_list) == word_type:
        print match(word_list, word_type)
        match(word_list, word_type)
What is this from? NLTK?
Having full context would be extremely useful
what is the book?
which Lesson?
The match function removes a word and returns it if the case matches. In the while loop in skip, you are printing a match, and then doing a match without printing. That's why it's printing every other word.
Oh, I think I got it, thanks buddy