我試圖用一個句子中的所有犯規詞替換成隨機字符。我將把它用於我的項目郵件。所以這就是我迄今所做的。Python:用一個句子中的字符替換犯規詞
curse=["apple","ball","car"]
fil = ["!","@","#","$","%","^","&","*","(",")"]
filword = ""
flag=0
word = raw_input(">>")
for each in curse:
if each == word:
worlen = len(word)
flag=1
if flag==1:
for c in fil:
if len(filword) != worlen:
filword+= c
word= word.replace(word, filword)
print word
假設那些來自列表詛咒的單詞是粗言穢語。 我已經可以將它翻譯成隨機字符了。 我的問題是如何從句子中替換犯規詞。 例子:
>> Apple you, Ball that car
我希望我的輸出是這樣的:
[email protected]#$% you, [email protected]#$ that [email protected]#
我怎麼能這樣做?謝謝! :)
大多數基本答案是使用'.split()'你的字符串,你的邏輯分別適用於每個字,然後加入'()'一切以句子 – Idos