![](/img/trans.png)
[英]Improve efficiency of python for loop counting items against IDs in nested list
[英]Improved efficiency of a nested for loop counting into a dictionary - Python
我正在尝试从更长的单词列表中过滤掉停用词列表,其中新过滤的单词及其计数成为字典的键值。 我的代码会这样做,但有两个问题:
我是不是在想这件事,还是有更快的方法来完成工作?
这是代码:
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.