首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Python:创建一个计算文本文件中特定单词数量的函数

Python:创建一个计算文本文件中特定单词数量的函数
EN

Stack Overflow用户
提问于 2019-10-21 17:38:53
回答 2查看 2K关注 0票数 0

我想创建一个函数来返回文本文件中特定单词的字数计数的值。

这是我目前所拥有的:

代码语言:javascript
代码运行次数:0
运行
复制
def Word_Counter(Text_File, Word):

    Data = open(Text_File, 'r').read().lower()

    count = Data.count(Word)


    print(Word, "; ", count)

Word_Counter('Example.txt', "the")

返回:"the;35“

这就是我想让它做的事情。但是,如果我想测试文本中的一系列单词,该怎么办呢?我想要列表或字典中的单词(键)和值。在不使用模块的情况下,有什么方法可以做到这一点?

假设我用这个单词列表测试了这个函数: time,when,left,I,do,an,who,what,time。

我希望得到的结果如下:

Word Counts = {'time': 1, 'when': 4, 'left': 0, 'I': 5, 'do': 2, 'an': 0, 'who': 1, 'what': 3, 'sometimes': 1}

我已经能够创建一个字典,对每个单词进行单词计数,如下例所示。

代码语言:javascript
代码运行次数:0
运行
复制
wordfreq = {}
for word in words.replace(',', ' ').split():
   wordfreq[word] = wordfreq.setdefault(word, 0) + 1

我想做一个类似的风格,但只针对特定的单词,有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-21 17:47:22

从您给定的代码中,我没有对此进行测试。

代码语言:javascript
代码运行次数:0
运行
复制
def Word_Counter(Text_File, word_list):

        Data = open(Text_File, 'r').read().lower()
        output = {}
        for word in word_list:
           output[word] = Data.count(Word)

或者你可以这样做

代码语言:javascript
代码运行次数:0
运行
复制
text = open("sample.txt", "r") 

# Create an empty dictionary 
d = dict() 

# Loop through each line of the file 
for line in text: 
    # Remove the leading spaces and newline character 
    line = line.strip() 

    # Convert the characters in line to  
    # lowercase to avoid case mismatch 
    line = line.lower() 

    # Split the line into words 
    words = line.split(" ") 

    # Iterate over each word in line 
    for word in words: 
        # Check if the word is already in dictionary 
        if word in d: 
            # Increment count of word by 1 
            d[word] = d[word] + 1
        else: 
            # Add the word to dictionary with count 1 
            d[word] = 1
票数 0
EN

Stack Overflow用户

发布于 2019-10-21 17:53:11

更新

尝试以下操作:

代码语言:javascript
代码运行次数:0
运行
复制
keywords = ['the', 'that']
worddict = {}

with open('out.txt', 'r') as f:
    text = f.read().split(' ')  # or f.read().split(',')

for word in text:
    worddict[word] = worddict[word]+1 if word in worddict else 1

print([{x, worddict[x]} for x in keywords])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58483592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档