在python中从列表元素中删除后缀
我必须创建一个程序,读取代码行,直到输入一个“.”,我必须删除标点符号,全部改为小写,删除停止字和后缀。我已经处理了所有这些,除了能够删除后缀外,我已经尝试了。正如您所看到的,strip,但它只接受一个参数,实际上并没有从列表元素中删除后缀。有什么建议/建议/帮助吗?谢谢在python中从列表元素中删除后缀,python,string,python-2.7,list,suffix,Python,String,Python 2.7,List,Suffix,我必须创建一个程序,读取代码行,直到输入一个“.”,我必须删除标点符号,全部改为小写,删除停止字和后缀。我已经处理了所有这些,除了能够删除后缀外,我已经尝试了。正如您所看到的,strip,但它只接受一个参数,实际上并没有从列表元素中删除后缀。有什么建议/建议/帮助吗?谢谢 stopWords = [ "a", "i", "it", "am", "at", "on", "in", "to", "too", "very", \ "of", "from", "here", "eve
stopWords = [ "a", "i", "it", "am", "at", "on", "in", "to", "too", "very", \
"of", "from", "here", "even", "the", "but", "and", "is", "my", \
"them", "then", "this", "that", "than", "though", "so", "are" ]
noStemWords = [ "feed", "sages", "yearling", "mass", "make", "sly", "ring" ]
# -------- Replace with your code - e.g. delete line, add your code here ------------
Text = raw_input("Indexer: Type in lines, that finish with a . at start of line only: ").lower()
while Text != ".":
LineNo = 0
x=0
y=0
i= 0
#creates new string, cycles through strint Text and removes puctutaiton
PuncRemover = ""
for c in Text:
if c in ".,:;!?&'":
c=""
PuncRemover += c
SplitWords = PuncRemover.split()
#loops through SplitWords list, removes value at x if found in StopWords list
while x < len(SplitWords)-1:
if SplitWords[x] in stopWords:
del SplitWords[x]
else:
x=x+1
while y < len(SplitWords)-1:
if SplitWords[y] in noStemWords:
y=y+1
else:
SplitWords[y].strip("ed")
y=y+1
Text = raw_input().lower()
print "lines with stopwords removed:" + str(SplitWords)
print Text
print LineNo
print x
print y
print PuncRemover
stopWords=[“a”、“i”、“it”、“am”、“at”、“on”、“in”、“to”、“too”、“very”\
“of”,“from”,“here”,“偶数”,“the”,“but”,“and”,“is”,“my”\
“他们”、“那么”、“这个”、“那个”、“比”、“虽然”、“所以”、“是”]
noStemWords=[“饲料”、“圣人”、“一岁”、“弥撒”、“制造”、“狡猾”、“戒指”]
#-----替换为您的代码-例如,删除行,在此处添加代码------------
Text=raw_input(“索引器:键入行,仅在行首处以“.”结尾:).lower()
而文本!=".":
LineNo=0
x=0
y=0
i=0
#创建新字符串,循环遍历strint文本并删除puccutaiton
PuncRemover=“”
对于文本中的c:
如果c在“,:;!?&”中:
c=“”
PuncRemover+=c
SplitWords=PuncRemover.split()
#循环通过SplitWords列表,如果在StopWords列表中找到,则删除x处的值
而x
以下函数应删除任何给定字符串中的后缀
from itertools import groupby
def removeSuffixs(sentence):
suffixList = ["ing", "ation"] #add more as nessecary
for item in suffixList:
if item in sentence:
sentence = sentence.replace(item, "")
repeatLetters = next((True for char, group in groupby(sentence)
if sum(1 for _ in group) >= 2), False)
if repeatLetters:
sentence = sentence[:-1]
return sentence
示例:
print(removeSuffixs("climbing running")) # 'climb run'
print(removeSuffixs("summation")) # 'sum'
在您的代码中,替换SplitWords[y]。strip(“ed”)
有,
SplitWords[y]=removesuffix(SplitWords[y])
您在这里只阅读了一次,请首先查看raw\u输入
有关代码样式的几点内容。你应该看一看。大写字通常保留给类或类型变量。另外,您的while
循环实际上应该是for
循环,因为您知道要执行多少次迭代。至于您的问题,您需要实际分配正在更改的列表元素。要剥离字符序列,请参见“读入行”是为了添加到字典中,这就是为什么现在它只读取一次。