为什么这个测试没有返回True?

时间:2015-12-12 03:42:42

标签: python string list

尝试将文本文件转换为字符串,这是我们老师给我们的测试,它应该打印为true:

test_text = """This is a small sentence. This isn't a small sentence, because
this sentence contains more than 10 words and a number! This isn't
a question, is it?"""

test_tm = TextModel( "Milestone test" )  # create a TextModel object

text = test_tm.readTextFromFile( "test.txt" )
print "Is text == test_text? ", text == test_text

这是我的代码:

class TextModel:

    def __init__(self, name):
        """ the constructor for the TextModel class
            all dictionaries are started at empty
            the name is just for our own purposes, to keep things 
            organized
        """
        self.name = name
        self.words = {}   # starts empty
        self.wordlengths = {}
        self.stems = {}
        self.sentencelengths = {}
        # you will want another dictionary for your text feature


    def __repr__(self):
        """ this method creates the string version of TextModel objects
        """
        s  = "\nModel name: " + str(self.name) + "\n"
        s += "    n. of words: " + str(len(self.words))  + "\n"
        s += "    n. of word lengths: " + str(len(self.wordlengths))  + "\n"
        s += "    n. of sentence lengths: " + str(len(self.sentencelengths))  + "\n"
        s += "    n. of stems: " + str(len(self.stems))  + "\n"
        # you will likely want another line for your custom text-feature!
        return s

    def readTextFromFile(self, filename):
        # f = open( filename, "r" )
        # text = f.read()
        # f.close()
        f = file( filename )
        text = f.read()
        words = text.split()
        return words

不确定为什么这些东西需要彼此相等,但重要的是它们在程序中更进一步。

2 个答案:

答案 0 :(得分:0)

readTextFromFile正在您所读取的字符串上调用.split(),因此它是" {"}的list。 (输入文件中的非空白文本段)。测试字符串只是一个你永远不会拆分的字符串。 list永远不会等于str

也许你打算测试一下?

text == test_text.split()

答案 1 :(得分:0)

由于您正在比较列表和字符串,因此它返回false。变量test_text包含一个字符串,但text包含test_tm.readTextFromFile()的返回值。返回值是单词列表,因为在TextModel.readTextFromFile的定义中,您返回words,这是读取的文本的拆分版本。您似乎只是返回错误的值。如果您将其更改为从函数而不是text返回words,则它应该像您期望的那样工作。