繁体   English   中英

提高嵌套 for 循环计数到字典中的效率 - Python

[英]Improved efficiency of a nested for loop counting into a dictionary - Python

我正在尝试从更长的单词列表中过滤掉停用词列表,其中新过滤的单词及其计数成为字典的键值。 我的代码会这样做,但有两个问题:

  1. 我想我听说嵌套的 for 循环是不受欢迎的,如果可能的话应该避免
  2. 循环似乎需要一段时间才能完成(16.89223 秒 - 在 2019 MacBook Pro 上)。 然而,结果有 3,476 个键值对。

我是不是在想这件事,还是有更快的方法来完成工作?

这是代码:

words_cleaned = [...] # a long list of words from a Shakespeare play

stop_words = ["i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your", "yours", "yourself", "yourselves", "he", "him", "his", "himself", "she", "her", "hers", "herself", "it", "its", "itself", "they", "them", "their", "theirs", "themselves", "what", "which", "who", "whom", "this", "that", "these", "those", "am", "is", "are", "was", "were", "be", "been", "being", "have", "has", "had", "having", "do", "does", "did", "doing", "a", "an", "the", "and", "but", "if", "or", "because", "as", "until", "while", "of", "at", "by", "for", "with", "about", "against", "between", "into", "through", "during", "before", "after", "above", "below", "to", "from", "up", "down", "in", "out", "on", "off", "over", "under", "again", "further", "then", "once", "here", "there", "when", "where", "why", "how", "all", "any", "both", "each", "few", "more", "most", "other", "some", "such", "no", "nor", "not", "only", "own", "same", "so", "than", "too", "very", "s", "t", "can", "will", "just", "don", "should", "now"]

go_word_counts = {}
go_words = []

for word in words_cleaned:
    if word not in stop_words:
        go_words.append(word)
    for word in go_words:
        if word not in go_word_counts:
            go_word_counts[word] = 1
        else:
            go_word_counts[word] += 1
       


    
go_word_counts

我感谢你的时间,内特

考虑使用Counter

from collections import Counter
res = Counter([word for word in words_cleaned if word not in stop_words])

暂无
暂无

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

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