python - 如何使用Python对字数进行排序?

标签 python sorting python-3.8 word-count

我正在使用此代码来计算文本文件中的相同单词。

filename = input("Enter name of input file: ")
file = open(filename, "r", encoding="utf8")
wordCounter = {}
with open(filename,'r',encoding="utf8") as fh:
  for line in fh:
    # Replacing punctuation characters. Making the string to lower.
    # The split will spit the line into a list.
    word_list = line.replace(',','').replace('\'','').replace('.','').replace("'",'').replace('"','').replace('"','').replace('#','').replace('!','').replace('^','').replace('$','').replace('+','').replace('%','').replace('&','').replace('/','').replace('{','').replace('}','').replace('[','').replace(']','').replace('(','').replace(')','').replace('=','').replace('*','').replace('?','').lower().split()
    for word in word_list:
      # Adding  the word into the wordCounter dictionary.
      if word not in wordCounter:
        wordCounter[word] = 1
      else:
        # if the word is already in the dictionary update its count.
        wordCounter[word] = wordCounter[word] + 1

print('{:15}{:3}'.format('Word','Count'))
print('-' * 18)
# printing the words and its occurrence.
for  word,occurance  in wordCounter.items():
  print(word,occurance)

我需要它们按从大到小的顺序排列作为输出。例如:

单词 1:25

单词 2:12

单词 3:5 。 。 .

我还需要将输入获取为“.txt”文件。如果用户写入任何不同的内容,程序必须收到错误“写入有效的文件名”。

如何对输出进行排序并同时生成错误代码?

最佳答案

要按顺序打印,您可以在打印之前按出现次数对它们进行排序,如下所示:

for  word,occurance  in sorted(wordCounter.items(), key=lambda x: x[1], reverse=True):
  print(word,occurance) 

为了按照您想要的方式检查文件是否有效,您可以考虑使用:

import os

path1 = "path/to/file1.txt"
path2 = "path/to/file2.png"

if not path1.lower().endswith('.txt'):
    print("Write a valid file name")

if not os.path.exists(path1):
    print("File does not exists!")

关于python - 如何使用Python对字数进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68445404/

相关文章:

python - 在 AWS Lambda 上为 python 3.8 导入请求

python - 如何在 Python 中转换指数并摆脱 'e+' ?

python - 如何反转数字的交替位

javascript - 为什么我无法按 2 个字段的差异对列表进行排序?

python - 困惑为什么在第二次评估不可变字符串的 += 运算符后不会更改 Python3 中的 id

python-3.x - 使用固定装置跳过 pytest 中的测试

python - 如何在 Keras 中获取图层的权重?

python - 如何改变NLTK中朴素贝叶斯分类器的平滑方法?

java - GWT - CellTable 可排序列不会响应点击

python - 在 Python 中根据属性对对象列表进行排序的更快方法