python-3.x 计算一个单词在文本文件中重复的次数

fdbelqdn  于 2022-12-05  发布在  Python
关注(0)|答案(1)|浏览(145)

我需要编写一个程序,提示输入文本文件的名称,并打印出出现频率最高和最低的单词,沿着它们的出现频率(用空格分隔)。
这是我的短信
你喜欢绿鸡蛋和火腿吗我不喜欢它们我不喜欢绿鸡蛋和火腿

file = open(fname,'r')
dict1 = []
for line in file:
  line = line.lower()
  x = line.split(' ')
  if x in dict1:
    dict1[x] += 1 
  else:
    dict1[x] = 1

然后,我想迭代键和值,找出最大频率和最小频率,但到那时,我的控制台显示“TypeError:列表索引必须是整数或切片,而不是列表”我也不知道这是什么意思
对于这个问题,预期结果是:

Max frequency: i 5
Min frequency: you 1
68de4m5k

68de4m5k1#

您使用的是列表而不是字典来存储词频。您不能像这样使用列表来存储键-值对,而需要使用字典来代替。下面是如何修改代码以使用字典来存储词频:

file = open(fname,'r')
word_frequencies = {} # use a dictionary to store the word frequencies

for line in file:
    line = line.lower()
    words = line.split(' ')
    for word in words:
        if word in word_frequencies:
            word_frequencies[word] += 1
        else:
            word_frequencies[word] = 1

然后迭代关键点并找到最小和最大频率

# iterate over the keys and values in the word_frequencies dictionary
# and find the word with the max and min frequency
max_word = None
min_word = None
max_frequency = 0
min_frequency = float('inf')

for word, frequency in word_frequencies.items():
    if frequency > max_frequency:
        max_word = word
        max_frequency = frequency
    if frequency < min_frequency:
        min_word = word
        min_frequency = frequency

打印结果

print("Max frequency:", max_word, max_frequency)
print("Min frequency:", min_word, min_frequency)

相关问题