使用loops-Python3从列表中删除标点符号
所以我要做的是创建一个函数,它读取一个列表,并使用循环创建一个没有标点符号的新列表。 到目前为止,我有:使用loops-Python3从列表中删除标点符号,loops,punctuation,Loops,Punctuation,所以我要做的是创建一个函数,它读取一个列表,并使用循环创建一个没有标点符号的新列表。 到目前为止,我有: list=["This:","is","a","list."] def depunctuate(): for i in range(0,len(list),1): list1="" for j in range(0,len(list[i]),1): if(list[i][j] !=['(',')','?',':',';',','
list=["This:","is","a","list."]
def depunctuate():
for i in range(0,len(list),1):
list1=""
for j in range(0,len(list[i]),1):
if(list[i][j] !=['(',')','?',':',';',',','.','!','/','"',"'"]):
list1+=list1[i][j]
cleanList+=[list1]
return cleanList
depunctuate()
因此,我希望它返回的是“这是一个列表”
不管我怎样
Traceback (most recent call last):
File "depunctuate.py", line 10, in <module>
depunctuate()
File "depunctuate.py", line 7, in depunctuate
tokens1 += tokens1[i][j]
IndexError: string index out of range
回溯(最近一次呼叫最后一次):
文件“depuncutate.py”,第10行,在
不守时
文件“depuncutate.py”,第7行,在depuncutate中
令牌s1+=令牌s1[i][j]
索引器错误:字符串索引超出范围
感谢您的帮助,谢谢
clean_l = [s.strip(".:;,'\"?!/") for s in l]
这将删除前导和尾随标点字符。我知道这会使标点字符更复杂,而且不必要,但我确实应该使用循环。这是我CS150课上的一个项目