将文件读入词典并保持计数 [英] Reading a File into a Dictionary And Keeping Count
问题描述
我有一个包含4个不同文章的文本文件,其中包含文字,每篇文章由文本分隔< NEW ARTICLE>
:
I have a text file with 4 different articles containing words in it, each article is separated by the text "<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>或ARTICLE>
。这样当我搜索单词黑暗和闪亮
它会进入第二和第三次< NEW ARTICLE>
出现
What I want to do is read this file and turn it into a dictionary, and then keep count of how many times "<NEW ARTICLE>" or "ARTICLE>"
is used. That way when I search for the words "dark and shiny"
it goes to the 2nd and 3rd time "<NEW ARTICLE>"
appears.
要搜索的词将是用户输入的变量,我想我可以弄清楚如何在文件中搜索它,我只是无法弄清楚如何将文件的内容转换成字典,然后每次保持计数< NEW ARTICLE>或ARTICLE>
出现,以便用户在文件中搜索单词时,会显示该单词存在的文章编号(可以是多个文章中单词的多个实例) )
The word to search for will be a user inputted variable, and I think I can figure out how to search for it in the file, I'm just having trouble figuring out how to turn the contents of the file into a dictionary and then keeping count everytime "<NEW ARTICLE>" or "ARTICLE>"
appears so that when a user searches for a word in the file, it displays the number of the article in which the word the exists (can be multiple instances of the word in multiple articles).
输出结果如下所示:
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,谢谢。
Using Python 3, thanks.
推荐答案
这个问题听起来像是我的家庭作业。所以我会给你一个算法,让你自己实现:
This question sounds like homework to me. So I will give you an algorithm and let you implement it yourself:
- 创建一个空字典
- 保持一个整数(让它调用它
articleNum
)。 - 通过输入文件迭代(首先打开它阅读,最好使用
与
) - 如果您看到的行包含
< NEW ARTICLE>
,则增加articleNum
。 li>
- 否则,迭代行中的单词(使用
line.split()
) - 对于该行中的每个单词,请检查该词是否是词典中的一个键。
- 如果字典中还没有键,请将其作为字典的键添加到字典中价值一个列表,其中包含值
articleNum
- 如果它已经是字典中的键,则追加
articleNum
到此键的值 - 完成阅读文件后,作为输入用户。
- 从字典获取用户输入的值(如果输入已经是字典中的键);这应该是一个整数列表
- 将这个整数列表打印出来,作为输出
- Create an empty dictionary
- Maintain an integer (lets call it
articleNum
). Start it at 0. - Iterate through the input file (open it for reading first, preferably using
with
) - If the line you see contains
<NEW ARTICLE>
, then incrementarticleNum
. - Else, iterate through the words in the line (use
line.split()
) - For each word in the line, check if that word is a key in the dictionary
- If it is not already a key in the dictionary, add it as a key to the dictionary and make it's value a list, that contains the value of
articleNum
- If it is already a key in the dictionary, then append
articleNum
to the value of this key - Once you are done reading the file, as the user for input.
- Get the value of the user's input from the dictionary (if the input is already a key in the dictionary); this should be a list of integers
- Print out this list of integers to the user, as output
希望这有助于
这篇关于将文件读入词典并保持计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!