将文件中的单词转换为Python中的列表

时间:2014-06-17 21:33:47

标签: python file

这不是一般性问题。我在http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/assignments/pset5.pdf

上做了MIT OCW 6.00问题5

他们使用Python 2.x而我下载了Python 3.4,我认为这可能是问题,但我不确定如何解决它。

我正在开发一个名为is_valid_word

的函数
def is_valid_word(word, hand, word_list):
    """
    Returns True if word is in the word_list and is entirely
    composed of letters in the hand. Otherwise, returns False.
    Does not mutate hand or word_list.

    word: string
    hand: dictionary (string -> int)
    word_list: list of lowercase strings
    """
    hand_copy = hand.copy()
    if word not in word_list:
        return False
    for char in word:
        if char not in hand_copy:
            return False
        else:
            hand_copy[char] -= 1
        if hand_copy[char] < 0:
            return False
    return True

测试用例在这里:

def test_is_valid_word(word_list):
    """
    Unit test for is_valid_word
    """
    failure=False
    # test 1
    word = "hello"
    hand = get_frequency_dict(word)

    if not is_valid_word(word, hand, word_list):
        print ("FAILURE: test_is_valid_word()")
        print ("\tExpected True, but got False for word: '" + word + "' and hand:", hand)

        failure = True

    # test 2
    hand = {'r': 1, 'a': 3, 'p': 2, 'e': 1, 't': 1, 'u':1}
    word = "rapture"

    if  is_valid_word(word, hand, word_list):
        print ("FAILURE: test_is_valid_word()")
        print ("\tExpected False, but got True for word: '" + word + "' and hand:", hand)

        failure = True        

    # test 3
    hand = {'n': 1, 'h': 1, 'o': 1, 'y': 1, 'd':1, 'w':1, 'e': 2}
    word = "honey"

    if  not is_valid_word(word, hand, word_list):
        print ("FAILURE: test_is_valid_word()")
        print ("\tExpected True, but got False for word: '"+ word +"' and hand:", hand)

        failure = True                        

    # test 4
    hand = {'r': 1, 'a': 3, 'p': 2, 't': 1, 'u':2}
    word = "honey"

    if  is_valid_word(word, hand, word_list):
        print ("FAILURE: test_is_valid_word()")
        print ("\tExpected False, but got True for word: '" + word + "' and hand:", hand)

        failure = True

    # test 5
    hand = {'e':1, 'v':2, 'n':1, 'i':1, 'l':2}
    word = "evil"

    if  not is_valid_word(word, hand, word_list):
        print ("FAILURE: test_is_valid_word()")
        print ("\tExpected True, but got False for word: '" + word + "' and hand:", hand)

        failure = True

    # test 6
    word = "even"

    if  is_valid_word(word, hand, word_list):
        print ("FAILURE: test_is_valid_word()")
        print ("\tExpected False, but got True for word: '" + word + "' and hand:", hand)
        print ("\t(If this is the only failure, make sure is_valid_word() isn't mutating its   inputs)")        

        failure = True        

    if not failure:
        print ("SUCCESS: test_is_valid_word()")

我获得FAILURE的唯一情况是应该返回True的情况。当我删除

if word not in word_list:
    return False

我得到了SUCCESS。但是,情况并非如此,因为其中一个要求是检查单词是否在word_list中。测试用例不包括单词列表中没有单词的情况,但是当我包含单词时,打印FAILURE。我认为问题在于列表word_list。 它来自这里:

WORDLIST_FILENAME = "words.txt"

def load_words():
    """
    Returns a list of valid words. Words are strings of lowercase letters.

    Depending on the size of the word list, this function may
    take a while to finish.
    """
    print ("Loading word list from file...")
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'rb', 0)
    # wordlist: list of strings
    wordlist = []
    for line in inFile:
       wordlist.append(line.strip().lower())
    print ("  ", len(wordlist), "words loaded.")
    return wordlist

分配inFile的行是我认为问题可能存在的地方,但我不知道如何修复它。

编辑:我刚刚意识到文件中的所有单词都是大写的,但测试用例是小写的。但是,

wordlist.append(line.strip().lower()) 

应该使文件中的单词小写吗?

0 个答案:

没有答案