繁体   English   中英

将文件读取循环变成列表推导

[英]Turning file reading for loops into list comprehensions

因此,我编写了一些代码,该代码确定了文本文件中的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))

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

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

通过最简单的理解,我们可以首先了解它们在展开时的外观。

通常,此形式的list理解:

result = []
for element in source:
    if predicate(element):
        result.append(modify(element))

可以简化为:

result = [modify(element) for element in source if predicate(element)]

这里的问题是我们要一次迭代两个元素,因为source的等效项是word_counts(most_common).total

因此,我们可以这样编写展开的for循环:

result = []
for word, freq in word_counts.most_common(total):
    if ((freq / total) * 100) >= 2:
        result.append((word, freq))

注意word, freq周围的多余一对括号; 形成一个tuple ,这是一个元素。 请记住,一次只能通过append将一个元素添加到list

这给我们以下理解:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM