将文件读入词典并保持计数 [英] Reading a File into a Dictionary And Keeping Count

查看:89
本文介绍了将文件读入词典并保持计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含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:


  1. 创建一个空字典

  2. 保持一个整数(让它调用它 articleNum )。

  3. 通过输入文件迭代(首先打开它阅读,最好使用

  4. 如果您看到的行包含< NEW ARTICLE> ,则增加 articleNum 。 li>
  5. 否则,迭代行中的单词(使用 line.split()

  6. 对于该行中的每个单词,请检查该词是否是词典中的一个键。

  7. 如果字典中还没有键,请将其作为字典的键添加到字典中价值一个列表,其中包含值 articleNum

  8. 如果它已经是字典中的键,则追加 articleNum 到此键的值

  9. 完成阅读文件后,作为输入用户。

  10. 从字典获取用户输入的值(如果输入已经是字典中的键);这应该是一个整数列表

  11. 将这个整数列表打印出来,作为输出

  1. Create an empty dictionary
  2. Maintain an integer (lets call it articleNum). Start it at 0.
  3. Iterate through the input file (open it for reading first, preferably using with)
  4. If the line you see contains <NEW ARTICLE>, then increment articleNum.
  5. Else, iterate through the words in the line (use line.split())
  6. For each word in the line, check if that word is a key in the dictionary
  7. 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
  8. If it is already a key in the dictionary, then append articleNum to the value of this key
  9. Once you are done reading the file, as the user for input.
  10. 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
  11. Print out this list of integers to the user, as output

希望这有助于

这篇关于将文件读入词典并保持计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆