第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

python 中的內(nèi)置函數(shù)輸入失敗

python 中的內(nèi)置函數(shù)輸入失敗

侃侃無極 2023-10-31 14:53:16
我試圖返回一個(gè)由每個(gè)關(guān)鍵字以及每個(gè)關(guān)鍵字及其同義詞在每個(gè)文檔中出現(xiàn)的次數(shù)組成的元組列表。當(dāng)輸入只是一個(gè)字符串(例如“happy”)時(shí),我沒有問題,但是當(dāng)我嘗試更多輸入(例如“happy”和“sad”)時(shí),代碼只打印最后一個(gè)字符串的輸出(“sad”) )這是我的代碼:class Entry :    def __init__(self, input_word, input_synonyms) :        self.word = input_word        self.synonyms = input_synonymse1 = Entry("sad", ["unhappy", "upset"])e2 = Entry("happy", ["cheerful", "joyful"])Thesaurus = [e1, e2]doc1 = ["the", "man", "is", "sad", "very", "sad", "and", "unhappy", "and", "upset"]doc2 = ["the", "boy", "is", "happy", "cheerful", "and", "joyful"]Corpus = [doc1, doc2]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, count])    return storeinput_ = "happy" and "sad"output = search(input_)print(output)控制臺(tái)輸出:[['sad', 2], ['unhappy', 1], ['upset', 1]]預(yù)期輸出:[['happy', 1], ['cheerful', 1], ['joyful', 1], ['sad', 2], ['unhappy', 1], ['upset', 1]]有什么辦法可以解決這個(gè)問題嗎?
查看完整描述

1 回答

?
月關(guān)寶盒

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊

接下來的代碼運(yùn)行良好:


class Entry :

    def __init__(self, input_word, input_synonyms) :

        self.word = input_word

        self.synonyms = input_synonyms


e1 = Entry("sad", ["unhappy", "upset"])

e2 = Entry("happy", ["cheerful", "joyful"])

Thesaurus = [e1, e2]

doc1 = ["the", "man", "is", "sad", "very", "sad", "and", "unhappy", "and", "upset"]

doc2 = ["the", "boy", "is", "happy", "cheerful", "and", "joyful"]

Corpus = [doc1, doc2]


def search(keyword) :

    all_words = ["happy", "cheerful", "joyful", "sad", "unhappy", "upset"]

    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, count])

    return store


input_ = ("happy", "cheerful", "joyful", "sad", "unhappy", "upset")

output = search(input_)

print(output)


控制臺(tái)輸出:


[['happy', 1], ['cheerful', 1], ['joyful', 1], ['sad', 2], ['unhappy', 1], ['upset', 1]]



查看完整回答
反對(duì) 回復(fù) 2023-10-31
  • 1 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)