我想嘗試在代碼3兩件事:Python 2.7版中刪除特定標點符號和禁用詞
- 刪除特定的標點符號
- 轉換輸入小寫
- 刪除禁用詞
我怎樣才能刪除標點而不使用「連接」。功能?我是新來的Python並沒有成功尚未使用的類似的方式去除禁用詞...
import string
s = raw_input("Search: ") #user input
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" ]
PunctuationToRemove = [".", ",", ":", ";", "!" ,"?", "&"]
while s != "":
s1 = ""
#Deleting punctuations and applying lowercase
for c in s: #for characters in user's input
if c not in PunctuationToRemove + " ": #characters that don't include punctuations and blanks
s1 = s + c #store the above result to s1
s1 = string.lower(s) #then change s1 to lowercase
print s1
爲什麼「而無需使用連接」?這是作業嗎,還是隻是抱怨加入?正則表達式怎麼樣?此外,你的'while'循環永遠不會改變's',並因此無限運行。 –
@tobias_k它只是我想管理我最近試圖吸收的主要功能:https://learnpythonthehardway.org/book/ –