2014-02-22 62 views
0

我想搜索輸入以查找詞列表。這到目前爲止有效。使用停用詞列表,然後打印觸發它的詞

swearWords = ["mittens", "boob"] 
phrase = raw_input('> ') 
listowords = [x.upper() for x in swearWords] 
if any(word in phrase.upper() for word in listowords): 
    print 'Found swear word!' 
else: 
    return 

現在讓我們來說說我想打印找到的單詞是什麼?

回答

0

下面是做這件事(遍歷不需要的話,檢查它是否存在):

undesired_words = ['blah', 'bleh'] 
user_input = raw_input('> ') 

for word in undesired_words: 
    if word in user_input.lower(): 
     print 'Found undesired word:', word 

print 'User input was OK.' 
+0

非常感謝!這很好。 – user2607110

+0

@ user2607110:歡迎來到SO!如果這個答案有幫助,你應該[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)。 – dparpyani

0

我已經修改了你的代碼的一些註釋了一下,檢查出來:

swearWords = ["mittens", "boob"] 
phrase = raw_input('> ') 

# Here I've split the phrase into words so we can iterate through 
phrase_words = phrase.split(' ') 

# Now we iterate through both the words and the swear words and see if they match 
for word in phrase_words: 
    for swear in swearWords: 
     if word.upper() == swear.upper(): 
      print "Found swear word:", swear 

這裏就是我得到的,當我運行它:

C:\Users\John\Desktop>temp.py 
> These are some swear words. Like boob or mittens maybe. Are those really swear 
words? 
Found swear word: boob 
Found swear word: mittens 

希望這有助於!

+0

謝謝!這很好。 – user2607110