Tôi có chuỗi người dùng đã nhập và tôi muốn tìm kiếm và thay thế bất kỳ lần xuất hiện nào của danh sách từ bằng chuỗi thay thế của tôi.Thay thế tất cả các từ trong danh sách từ bằng một chuỗi khác trong python
import re
prohibitedWords = ["MVGame","Kappa","DatSheffy","DansGame","BrainSlug","SwiftRage","Kreygasm","ArsonNoSexy","GingerPower","Poooound","TooSpicy"]
# word[1] contains the user entered message
themessage = str(word[1])
# would like to implement a foreach loop here but not sure how to do it in python
for themessage in prohibitedwords:
themessage = re.sub(prohibitedWords, "(I'm an idiot)", themessage)
print themessage
Mã trên không hoạt động, tôi chắc rằng tôi không hiểu cách thức hoạt động của vòng lặp.
Bạn nên thử kiểm tra thực hiện SpamBayes cho python có thể được mở rộng hơn. – dusual