显示Python字典中查询文件中每行单词的次数

2024-05-08 13:09:19 发布

您现在位置: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,我正在尝试这样做: 对于第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}

我怎样才能解决这个问题


2条回答

下面的代码将计算给定文件中的每个单词

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

一个简单的方法是使用collections.Counter

from collections import Counter

with open("file.txt","r") as f:
    lineList = f.readlines()
for i,k in enumerate(map(lambda x:x.strip().split(" "),lineList)): # split each word
    print(f"Line {i+1}:{Counter(k)}")

结果:

Line 1:Counter({'urgently': 3, 'joyously': 1, 'truthfully': 1, 'seemingly': 1, 'broadly': 1, 'knowingly': 1, 'steadily': 1})
Line 2:Counter({'rigidly': 2, 'joyously': 1, 'urgently': 1, 'truthfully': 1, 'seemingly': 1, 'broadly': 1, 'suddenly': 1, 'healthily': 1, 'commonly': 1, 'often': 1})
Line 3:Counter({'totally': 2, 'tremendously': 1, 'steadily': 1, 'sharply': 1})

或者你可以用口述:

l = ['joyously', 'urgently', 'truthfully', 'seemingly', 'broadly', 'knowingly', 'steadily', 'rigidly', 'suddenly', 'healthily', 'commonly', 'often', 'tremendously', 'totally', 'sharply']
with open("file.txt","r") as f:
    lineList = f.readlines()

for line,i in enumerate(map(lambda x:x.strip().split(" "),lineList)):
    d = dict.fromkeys(l, 0) # load a dict from the list
    for j in i:
        d[j] += 1
    print(f"For line {line+1}:{d}")

结果:

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}
For line 2:{'joyously': 1, 'urgently': 1, 'truthfully': 1, 'seemingly': 1, 'broadly': 1, 'knowingly': 0, 'steadily': 0, 'rigidly': 2, 'suddenly': 1, 'healthily': 1, 'commonly': 1, 'often': 1, 'tremendously': 0, 'totally': 0, 'sharply': 0}
For line 3:{'joyously': 0, 'urgently': 0, 'truthfully': 0, 'seemingly': 0, 'broadly': 0, 'knowingly': 0, 'steadily': 1, 'rigidly': 0, 'suddenly': 0, 'healthily': 0, 'commonly': 0, 'often': 0, 'tremendously': 1, 'totally': 2, 'sharply': 1}

相关问题 更多 >