简体   繁体   English

如何使用“in”从列表中删除某些单词 function

[英]how can I remove certain words from a list using the "in" function

def calculate_frequencies(file_contents):
    punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    uninteresting_words = ["the", "a", "to", "if", "is", "it", "of", "and", "or", "an",      "as", "i", "me", "my", \
    "we", "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", "hers", "its", "they", "them", \
    "their", "what", "which", "who", "whom", "this", "that", "am", "are", "was", "were", "be", "been", "being", \
    "have", "has", "had", "do", "does", "did", "but", "at", "by", "with", "from", "here", "when", "where", "how", \
    "all", "any", "both", "each", "few", "more", "some", "such", "no", "nor", "too", "very", "can", "will", "just"]
    for punctuation in punctuations:
         file_contents = file_contents.replace(punctuation,"")
        
    
   
    file_contents = file_contents.lower()
    file_contents = file_contents.split()
    
    for word in file_contents:
        if word in uninteresting_words:
            file_contents.remove(word)
    
    word_count = {}
    for word in file_contents:
        if word not in word_count.keys():
            word_count[word]= 1
        else :
            word_count[word]+= 1
    return print(file_contents)
calculate_frequencies(file_contents)

here is my code, file_contents is a.txt file where I need to filter out all punctuations and all uninteresting words.这是我的代码,file_contents 是一个.txt 文件,我需要在其中过滤掉所有标点符号和所有无趣的单词。 I got all the punctuations out but I am having trouble removing the uninteresting words.我把所有的标点符号都去掉了,但我无法删除无趣的词。 I feel like this should already do the job but when I run the code there are still uninteresting words around, what am I doing wrong?我觉得这应该已经可以完成工作了,但是当我运行代码时,周围仍然有一些无趣的话,我做错了什么?

Instead of trying to remove the uninteresting words, you could simply ignore them in your for loop like this:您可以像这样在 for 循环中简单地忽略它们,而不是尝试删除无趣的词:

def calculate_frequencies(file_contents):
    punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    uninteresting_words = ["the", "a", "to", "if", "is", "it", "of", "and", "or", "an", "as", "i", "me", "my", \
                           "we", "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", "hers", "its",
                           "they", "them", \
                           "their", "what", "which", "who", "whom", "this", "that", "am", "are", "was", "were", "be",
                           "been", "being", \
                           "have", "has", "had", "do", "does", "did", "but", "at", "by", "with", "from", "here", "when",
                           "where", "how", \
                           "all", "any", "both", "each", "few", "more", "some", "such", "no", "nor", "too", "very",
                           "can", "will", "just"]
    for punctuation in punctuations:
        file_contents = file_contents.replace(punctuation, "")

    file_contents = file_contents.lower()
    file_contents = file_contents.split()
    
    word_count = {}
    for word in file_contents:
        if word not in uninteresting_words:
            if word not in word_count.keys():
                word_count[word] = 1
            else:
                word_count[word] += 1
    return print(file_contents)


calculate_frequencies(file_contents)

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

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