【发布时间】:2016-12-02 00:59:41
【问题描述】:
假设我有以下变量...
bad_words = ['bad0', 'bad1', 'bad2']
bad_string = "This string has bad1 in it."
bad_string2 = "This string is abcbad0xyz."
good_string = "This string is good!"
在字符串中查找“坏词”并只打印出好字符串的最佳方法是什么?
示例...
def check_words(string):
bad_words = ['bad0', 'bad1', 'bad2']
#this is where I need help...
#Return False if string contains any of the words in bad words
#Return True if string does not contain bad words.
bad_string = "This string has bad1 in it."
good_string = "This string is good!"
#call the check_words method by sending one of the strings
valid = check_words(bad_string) #I want this to return False
if valid:
print("Good string!")
else:
print("Bad string!")
#or...
valid = check_words(good_string) #I want this to return True
if valid:
print("Good string!")
else:
print("Bad string!")
【问题讨论】:
标签: python arrays string python-3.4