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

rslzwgfq  于 2022-12-15  发布在  Python
关注(0)|答案(1)|浏览(123)

我需要编写一个程序,提示输入文本文件的名称,并打印频率最高和最低的单词,沿着它们的频率(用空格分隔)。
这是我的短信

I am Sam
Sam I am
That Sam-I-am
That Sam-I-am
I do not like
that Sam-I-am
Do you like
green eggs and ham
I do not like them
Sam-I-am
I do not like
green eggs and ham

代码:

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
bvn4nwqk

bvn4nwqk1#

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

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)

相关问题