简体   繁体   English

您如何查看内置变量以检查您的答案是否正确? Python

[英]How do you see built-in variables to check if your answer is correct? Python

I'm almost there, I think, but I'm stuck.我想我快到了,但我被困住了。 In this problem, there are built-in variables, Thesaurus and Corpus, that I don't have to define.在这个问题中,我不必定义内置变量,词库和语料库。 I'm having a couple issues:我有几个问题:

  1. I'm not getting the right keywords and/or occurrences from my code我没有从我的代码中获得正确的关键字和/或出现次数
  2. I can't figure out how to correctly print the Thesaurus (in order to check).我不知道如何正确打印同义词库(以便检查)。 The Corpus prints nicely with just a simple print statement, however the same print statement will return items in the Thesaurus as their locations instead of the words.语料库只需一个简单的 print 语句就可以很好地打印,但是相同的 print 语句将返回同义词库中的项目作为它们的位置而不是单词。 I don't know the variable attributes, as I didn't define the class, so I feel like I'm shooting in the dark.我不知道变量属性,因为我没有定义class,所以我觉得我在黑暗中拍摄。
  3. When I try to give store.append two arguments, it won't accept both, even though I see other people have done it this way.当我尝试给 store.append 两个 arguments 时,它不会同时接受这两个,即使我看到其他人已经这样做了。 I can only get the words and number of occurrences to print when I have two store.append statements.当我有两个 store.append 语句时,我只能得到要打印的单词和出现次数。 (I'm trying to get the output to be a tuple and not a list) (我试图让 output 成为一个元组而不是一个列表)

Right now, I'm testing with this made-up Thesaurus and Corpus:现在,我正在使用这个虚构的词库和语料库进行测试:

    class Entry : #testing your own Thesaurus/ Corpus
        def __init__(self, word, synonyms) :
            self.word = word
            self.synonyms = synonyms

    e1 = Entry("savory", ["umami", "meat", "main course", "dinner"])
    e2 = Entry("sweet", ["dessert", "candy", "tart", "sugar"])
    Thesaurus = [e1, e2] 

    doc1 = ["My", "main course", "is", "a", "meat", "dish", "with", "lots", "of", "umami"]
    doc2 = ["yum", "I", "love", "sweet", "dessert"]
    doc3 = ["this", "is", "yet", "another", "savory", "dish"]
    Corpus = [doc1, doc2, doc3] 

Any help is appreciated.任何帮助表示赞赏。 Thanks.谢谢。

def search(keyword) :

    all_words = [keyword]
    for entry in Thesaurus:
        if entry.word == keyword:
            for word in entry.synonyms:
                all_words.append(word)
    store = []
    
    for search_word in all_words:
        count = 0
        for document in Corpus:
            for word in document:
                if search_word == word:
                    count = count + 1
        store.append(search_word)
        store.append(count)

    return store

input = "happy"
output = search(input) # invoke the method using a test input
print(output) # prints the output of the function
# do not remove this line!
  1. I think the best solution for you is using the dictionary as the representation of the Entity if you don't have to use class, which look like e1 = {'savory':["umami", "meat", "main course", "dinner"]}.我认为对您来说最好的解决方案是使用字典作为实体的表示,如果您不必使用 class,它看起来像 e1 = {'savory':["umami", "meat", "main course" , “晚餐”]}。

  2. You have two ways to print class:您有两种打印 class 的方法:

2.1 write a print method in the class and call it to print the class. 2.1 在class中写一个打印方法,调用它来打印class。

2.2 try to visit __ dict __ like: print(e1.__ dict __) (no blankspace) 2.2 尝试访问__ dict __ like: print(e1.__ dict __) (没有空格)

  1. append two args by using a.apend([b1, b2]) is a possible way. append 两个 args 使用 a.apend([b1, b2]) 是一种可能的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM