我有包含在裏面的話4篇不同的文章,每篇文章由文本"<NEW ARTICLE>"
分離的文本文件:讀入文件到一個字典,並保持計數
<NEW ARTICLE>
Take a look at
what I found.
<NEW ARTICLE>
It looks like something
dark and shiny.
<NEW ARTICLE>
But how can something be dark
and shiny at the same time?
<NEW ARTICLE>
I have no idea.
我想要做的就是閱讀本文件並將其變成字典,然後保持使用次數"<NEW ARTICLE>" or "ARTICLE>"
。這樣,當我搜索單詞"dark and shiny"
它進入第二次和第三次出現"<NEW ARTICLE>"
。
要搜索的詞將是一個用戶輸入的變量,我想我可以找出如何在文件中搜索它,我只是無法弄清楚如何將文件的內容變成詞典,然後每次保持計數"<NEW ARTICLE>" or "ARTICLE>"
出現,以便當用戶搜索文件中的單詞時,它會顯示單詞所在的文章的編號(可以是多個文章中單詞的多個實例)。
輸出會是這個樣子:
Input - Word(s) to search for: dark and shiny
Output - Word(s) found in articles: 2 3
Input - Read which article?: 2
Output - It looks like something dark and shiny.
使用Python 3,謝謝。
氣味像功課......是什麼呢? – inspectorG4dget
這是,但我不需要有人爲我做,只是朝着正確的方向邁出的一步。我想我可以弄清楚它的大部分內容,只需從格式化爲字典開始。 – Goose