python - 使用python仅计算文本文件中的每个单词一次

标签 python algorithm dictionary iteration defaultdict

我有一个小的 python 脚本,我正在为类作业编写。该脚本读取一个文件并打印 10 个最常用和最不常用的单词及其频率。对于此作业,一个单词定义为 2 个或更多字母。我的词频工作得很好,但是作业的第三部分是打印文档中独特 词的总数。独特的单词意味着计算文档中的每个单词,只计算一次。

如何在不对当前脚本进行过多更改的情况下,将文档中的所有单词只统计一次?

附注我使用的是 Python 2.6,所以请不要提及 collections.Counter 的使用

from string import punctuation
from collections import defaultdict
import re

number = 10
words = {}
total_unique = 0
words_only = re.compile(r'^[a-z]{2,}$')
counter = defaultdict(int)


"""Define words as 2+ letters"""
def count_unique(s):
    count = 0
    if word in line:
        if len(word) >= 2:
            count += 1
    return count


"""Open text document, read it, strip it, then filter it"""
txt_file = open('charactermask.txt', 'r')

for line in txt_file:
    for word in line.strip().split():
        word = word.strip(punctuation).lower()
        if words_only.match(word):
               counter[word] += 1


# Most Frequent Words
top_words = sorted(counter.iteritems(),
                    key=lambda(word, count): (-count, word))[:number] 

print "Most Frequent Words: "

for word, frequency in top_words:
    print "%s: %d" % (word, frequency)


# Least Frequent Words:
least_words = sorted(counter.iteritems(),
                    key=lambda (word, count): (count, word))[:number]

print " "
print "Least Frequent Words: "

for word, frequency in least_words:
    print "%s: %d" % (word, frequency)


# Total Unique Words:
print " "
print "Total Number of Unique Words: %s " % total_unique

最佳答案

计算 counter 字典中 key 的数量:

total_unique = len(counter.keys())

或者更简单地说:

total_unique = len(counter)

关于python - 使用python仅计算文本文件中的每个单词一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12504477/

相关文章:

algorithm - 将 Int 均匀随机范围缩放为 Double one

javascript - ES6 按数组中的对象属性求和

swift - 有没有办法遍历 Key :[Value1:[Value2]]? 形式的字典

c# linq filter Dictionary<DateTime, int> 按预定义的最小时间差

python - cv2.error:OPENCV(4.4.0)错误(-215声明失败)size.height> 0 && size,width> 0)

algorithm - 使用什么样的算法来分解数据?

javascript - 从 Javascript onclick() 函数中抓取有关爬行和信息的建议

重复背包算法

python - 替换 NumPy 数组的某些给定索引的最有效方法是什么?

python - 在 Python 中,如何使用 'update_annotations' 更新 plotly 数字?