2015-12-14 55 views
0

我在制作一个Scrabbler Helper程序。将文件保存为数据结构,并保留为类的属性

在此作业中,在一个类中包含Scrabbler Dictionary,创建一个类来管理Scrabble字典。

及其INIT()方法应该阅读words.txt文件到保持作为类的属性的适当的数据结构。

我不确定这是什么意思?

+0

这意味着,你需要看到words.txt是如何构成的。如果是以新行分隔的单词列表。 [list](https://docs.python.org/2/tutorial/datastructures.html)是适当的数据结构。 – mohit

+0

这是一个单词列表。我们从来没有被教过如何获得init()方法来读取文件。 words.txt文件是由新行分隔的单词列表。我不知道如何将words.txt文件读入列表(?)。这个列表应该是班级的一个属性? –

回答

0

如果您需要创建ScrabblerDictionary的多个实例,最好在创建实例之前将文件读入列表中。

这里是一个小片段,您可以使用:

class ScrablerDictionary: 
    def __init__(self, words): 
     self.words = words 


    def main(): 
    with open('words.txt') as f: 
     words = f.read().splitlines() 

    scrabler = ScrablerDictionary(words) 
    print(scrabler.words) 


    if __name__ == "__main__": 
    main() 
+0

好的。谢谢! –