Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't understand
#1
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)
Reply
#2
What is this from? NLTK?
Having full context would be extremely useful
what is the book?
which Lesson?
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Oh, I think I got it, thanks buddy
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020