Python 如何确定一个元素列表是否在另一个元素列表中?

Python 如何确定一个元素列表是否在另一个元素列表中?,python,list,text,replace,element,Python,List,Text,Replace,Element,我上传了一个文本文件作为文件内容。这样做的目的是在删除标点符号和一些“无趣单词”后,计算文本文件中出现的单词。下面是我的代码: def calculate_frequencies(file_contents): # Here is a list of punctuations and uninteresting words you can use to process your text punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*

我上传了一个文本文件作为文件内容。这样做的目的是在删除标点符号和一些“无趣单词”后,计算文本文件中出现的单词。下面是我的代码:

def calculate_frequencies(file_contents):
# Here is a list of punctuations and uninteresting words you can use to process your text
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"]
# LEARNER CODE START HERE

file_list = file_contents.split() #split words into individual string

for word in file_list:
    word= word.lower()#small letter

    if word in uninteresting_words:
        word = word.replace(word,"")
        return word
    if word in punctuations:
        word = word.replace(word,"")
        return word
print (word)

#create a dictionary
#add those words if they are not in dictionary , and count+1 for value
dict={}
n=0
for x in word:
    if x not in dict:
        n=1
        dict[x]=n
    else:
        n+=1
        dict[x]=n
    return dict
print(dict)
def计算频率(文件内容):
#下面是一个标点符号和无趣单词的列表,您可以使用它们来处理文本
标点符号=“”!()-[]{};:'"\,./?@#$%^&*_~'''
无趣的单词=[“the”,“a”,“to”,“if”,“is”,“it”,“of”,“and”,“or”,“an”,“as”,“i”,“me”,“my”\
“我们”、“我们的”、“我们的”、“你”、“你的”、“你的”、“他”、“她”、“他”、“他的”、“她”、“她的”、“它”、“它们”、“它们”\
“他们”、“什么”、“哪个”、“谁”、“谁”、“这个”、“那个”、“我”、“是”、“是”、“是”、“是”、“是”、“是”、“被”、“被”、“被”、“存在”\
“have”,“has”,“had”,“do”,“does”,“did”,“but”,“at”,“by”,“with”,“from”,“here”,“when”,“where”,“how”\
“全部”、“任何”、“两者”、“各自”、“很少”、“更多”、“一些”、“这样”、“不”、“也”、“非常”、“可以”、“将”、“只是”]
#学习者代码从这里开始
file_list=file_contents.split()#将单词拆分为单个字符串
对于文件列表中的word:
word=word.lower()#小写字母
如果单词是无趣的单词:
word=word。替换(word,“”)
回信
如果单词中有标点符号:
word=word。替换(word,“”)
回信
打印(word)
#创建字典
#如果这些单词不在字典中,则添加它们,并计算+1作为值
dict={}
n=0
对于word中的x:
如果x不在dict中:
n=1
dict[x]=n
其他:
n+=1
dict[x]=n
返回指令
打印(dict)
计算\u频率(文件内容)


结果显示为“”。我可以知道我的代码有什么问题吗

from collections import Counter
from string import punctuation

# Remove punctuation from file_content
file_content = file_content.translate(str.maketrans("", "", punctuation))
# Get words
words = file_content.split()
# Keep only some words
words = [word for word in words if word not in uninteresting_words]
# Create the dictionary
d = Counter(words)

你想用这段代码实现什么?给出示例输入和输出。我想删除所有标点符号和无趣的单词,并生成一个单词列表。很抱歉,看起来很混乱,我将编辑我的代码尝试删除
返回词
行。适当的缩进将有所帮助,不知道函数的结尾。我只是编辑post、 我试图删除,但只得到列表“单词”只显示一个单词,这是不可能的,因为我的文本文件有很多单词