首页 > 解决方案 > 将文件读取 for 循环转换为列表推导

问题描述

所以我写了一些代码来确定文本文件中最常见的 4 个单词,然后找出所有出现 2% 或更多的单词。到目前为止,我的代码运行良好。但我必须将 for 循环转换为列表推导。

到目前为止,我已经尝试过:

percent_list = [word, freq in word_counts.most_common(total) if ((freq/total)*100) >= 2.0]  

对于第二个 for 循环,(请参阅下面的整个代码。)但它不起作用。这对于列表理解来说似乎有点长,因为所有在线的似乎都短了很多。

这是整个程序。总共有两个for循环。

from collections import Counter
from operator import itemgetter

STOP = ["the", "and", "in", "to", "a", "of", "at", "it", "but", "its","it's", "that", "was", "with", "as", "are", "i","this", "for", "if"]



word_counts = Counter()

with open("file.txt") as f:
  for token in f.read().split():
    if token.lower() not in STOP:
      word_counts[token.lower()] += 1

  print( word_counts.most_common(4),  ":")  


total = sum(word_counts.values())

print("\nWords that occur for 2% or more are: ")
for word, freq in word_counts.most_common(total):
  if ((freq/total)*100) >= 2.0:
    print("\n {} ".format(word))

标签: pythonfor-looplist-comprehension

解决方案


我认为这应该可以解决您的问题。它将返回单词和频率的元组列表。

percent_list = [(word, freq) for word,freq in word_counts.most_common(total) if ((freq/total)*100) >= 2.0]  

推荐阅读