python - 显示字典Python中查询文件中每行单词的次数
问题描述
我有一个 docs.txt 作为文件,注意分开的三行:
joyously urgently truthfully seemingly broadly urgently knowingly urgently steadily
joyously urgently truthfully seemingly rigidly broadly rigidly suddenly healthily commonly often
tremendously totally steadily sharply totally
我有一个带有此文件的querys.txt:
urgently
rigidly suddenly
totally steadily
和代码:
dictionary = {}
angledictionary={}
document= open('docs.txt', 'r')
for line in document:
lined=line.split()
for word in lined:
if word not in dictionary.keys():
dictionary[word]=0
dictionary[word]+=1
dictionary=dict.fromkeys(dictionary,0)
with open("queries.txt", "r") as open_queries:
searchquery = open_queries.read().split("\n")
with open('docs.txt', 'r') as openrelevancy:
words = openrelevancy.read().split("\n")
for query in searchquery:
print('Query:', query)
relevant = []
line_number = 0
for word in words:
line_number += 1
if query in word:
relevant.append(line_number)
print('Relevant Documents:', *relevant)
现在,每行的字典字数为 0,我正在尝试这样做: FOR LINE 1:
{'joyously': 1, 'urgently': 3, 'truthfully': 1, 'seemingly': 1, 'broadly': 1, 'knowingly': 1, 'steadily': 1, 'rigidly': 0, 'suddenly': 0, 'healthily': 0, 'commonly': 0, 'often': 0, 'tremendously': 0, 'totally': 0, 'sharply': 0}
对于第 2 行:
{'joyously': 1, 'urgently': 1, 'truthfully': 1, 'seemingly': 1, 'broadly': 1, 'knowingly': 1, 'steadily': 1, 'rigidly': 2, 'suddenly': 1, 'healthily': 1, 'commonly': 1, 'often': 1, 'tremendously': 0, 'totally': 0, 'sharply': 0}
对于第 3 行:
{'joyously': 0, 'urgently': 0, 'truthfully': 0, 'seemingly': 0, 'broadly': 0, 'knowingly': 0, 'steadily': 0, 'rigidly': 0, 'suddenly': 0, 'healthily': 0, 'commonly': 0, 'often': 0, 'tremendously': 1, 'totally': 2, 'sharply': 1}
我该如何解决这个问题?
解决方案
下面的代码将计算给定文件中的每个单词。
import os
def to_dict(file_path, delimiter=" "):
dict_list = [] # Each index is a line
if os.path.exists(file_path):
with open(file_path, 'rb') as in_file:
for line in in_file.readlines(): # Grab each line as a list
line = line.split(delimiter) # Split at our delim
new_dict = {}
for word in line: # Code to count each index
word = word.rstrip() # Remove formatting
if word in new_dict:
new_dict[word] += 1
else:
new_dict[word] = 1
dict_list.append(new_dict)
return dict_list
else:
print("{} Does not exist. Check the path and try again".format(file_path))
dict_count = to_dict(the_file_path, " ")
if dict_count: # We found and converted file to a dict
# function to query against words
推荐阅读
- r - 如何将对象分配给函数的输出?
- angular - 角度报告当前路由和参数到任何组件?
- r - 使用源代码时在 ShinyApp 中找不到函数
- python - TypeError:“类型”类型的参数不可迭代
- c# - Azure 部署后 ASP.NET Core MVC webapp 错误
- typescript - TypeScript:可选的合并类型
- sql - 使用 Postgres,如果为 NULL,我如何使用 UPDATE 插入新的 jsonb 值,或者如果值已经存在,如何修改 json?
- c# - 无法使用单例“IMyCustomThing”中的范围服务“IHttpContextAccessor”
- reactjs - React - 使用 POST 请求从服务器下载文档
- python - 我如何在 Windows 10 上为 python 3.8.5 安装 pyaudio