我想知道的是如何在方法中实现类元组'excludedWords‘,这样方法就会忽略它们。我尝试使用split方法,并尝试在该split方法中调用元组,无论它是self.excludedWords还是Prose.excludedWords。
class Prose:
excludedWords = ('is', 'the', 'are', 'we', 'they', 'it', 'he', 'she')
def __init__(self, words):
self.prose = words
def __str__(self):
return str(self.prose)
def numberOfWords(self):
return len(self.prose.split())
def distinctWords(self):
return len(self.prose.split(self.prose))
def toString(self):
for words in self.prose.split():
print(words)
def __sub__(self, other):
if self.prose != other.prose:
newprose = self.prose.split(other.prose)
otherprose = other.prose.split(self.prose)
return newprose
else:
return self.prose
def newDict(self):
myDict = {}
myProse = self.prose.split()
for word in myProse:
if word in myDict:
myDict[word] += 1
else:
myDict[word] = 1
return myDict发布于 2019-10-13 09:58:06
def __init__(self, words):
self.prose = words
self.filtered_prose = ' '.join([w for w in words.split() if w not in excludedWords])这将过滤掉您想要在开头排除的任何单词。请注意,如果标点符号跟在排除的单词后面,如"is,",则不会排除该单词。
https://stackoverflow.com/questions/58359943
复制相似问题