在循环内迭代变量名

我几乎没有编程的实践经验,但我已经开始学习 python 并想创建一个函数来计算文本中最常用的单词。现在,我确信我的版本不是最好的方法,但它有效:


 import os


 punctuation = "~!@#$%^&*()_-=+[{]}\\|'\";:,<.>/?"


 def remove_punctuation(text):


     text_wo_punctuation = ""

     for word in text:

         if word not in punctuation:

             text_wo_punctuation += word

     return text_wo_punctuation


 with open(r'New Text Document.txt') as f:


     text = f.read().lower()

     t = remove_punctuation(text).split()

     dictionary = {}

     for word in t:

         if word in dictionary:

             dictionary[word] = dictionary[word] + 1

         else:

             dictionary[word] = 1


 print(dictionary)


 def top_five(d):


     top = {}

     value1 = 0

     value2 = 0

     value3 = 0

     value4 = 0

     value5 = 0



     for key in dictionary:

         if value1 < dictionary[key] and key not in top:

             value1 = dictionary[key]

             top1 = {key:value1}

         else:

             continue

     top.update(top1)    

     for key in dictionary:

         if value2 < dictionary[key] and key not in top:

             value2 = dictionary[key]

             top2 = {key:value2}

         else:

             continue

     top.update(top2)

     for key in dictionary:

         if value3 < dictionary[key] and key not in top:

             value3 = dictionary[key]

             top3 = {key:value3}

         else:

             continue

     top.update(top3)

     for key in dictionary:

         if value4 < dictionary[key] and key not in top:

             value4 = dictionary[key]

             top4 = {key:value4}

         else:

             continue

     top.update(top4)

     for key in dictionary:

这段代码将创建一个包含 value1、value2 等的字典,我可以在我的循环中使用它以及另一个包含 top1、top2 等的字典,但它不起作用,因为“并且键不在顶部”将不起作用。


top["top"+str(i)] = {key:values["value"+str(i)]}

这将在字典中创建一个字典。我被困在这里,因为我找不到使“顶级”字典有用的方法,或者在循环中迭代变量名。我读过应该使用列表或字典,并且变量名迭代不是一个好主意,但我不明白为什么会这样,我想不出一种方法来使列表或字典在我的 for 循环中有用。


正如我所说,我知道这可能不是制作这种功能的最佳方法,但我的问题是:如何简化我已经制作的功能并使循环正常工作?


森林海
浏览 213回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python