【发布时间】:2020-07-07 03:43:41
【问题描述】:
我已将一个文本文件上传为 file_contents。这样做的目的是在删除标点符号和一些“无趣的单词”后计算出现在我的文本文件中的单词。以下是我的代码:
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)
计算频率(文件内容)
结果显示 '' 。我可以知道我的代码有什么问题吗
【问题讨论】:
-
你想用这段代码实现什么?给出示例输入和输出。
-
我想删除所有的标点符号和 uninteresting_words 并生成一个单词列表。抱歉,它看起来令人困惑,我将编辑我的代码
-
尝试删除
return word这两条线。 -
适当的缩进会有帮助,不知道你的函数在哪里结束。
-
我只是编辑帖子。我试图删除但只得到列表“单词”只显示一个单词,这是不可能的,因为我的文本文件有很多单词
标签: python list text replace element