Set flag
value to True
when message contains any items from the list block
.
- Get
block
list and iterate every item from the list by for
loop.
- Set value of
flag
to False
by default means message not contains any block list item.
- Set value of
flag
to True
when item from the block
list in the message.
- Use
break
statement to stop for
loop iteration.
e.g.
>>> block = ["damn", "shit"]
>>> msg = "test masf to check shit or damn"
>>> flag = False
>>> for i in block:
... if i in msg:
... flag = True
... break
...
>>> flag
True
>>>
Updated:
- Set
block
list words.
- Create list of list of block list word characters.
- Remove punctuation from the input string.
- split string to words.
- split word and create sequence list of characters from the word.
- check sequence list of characters present in block list character list.
code:
import string
block = ["damn", "shit"]
block_char_list = [ list(i) for i in block ]
def getbBlockListFlag(msg):
#Remove punctuation
tmp = msg.translate(string.maketrans("",""), string.punctuation)
tmp1 = []
for word in tmp.split():
j = []
for k in list(word):
if k not in j:
j.append(k)
elif k != j[-1]:
j.append(k)
# word in block list
if j in block_char_list:
return True, j
return False, []
msg = "True test case ? daaaammmmnnnnn.,"
status, block_char = getbBlockListFlag(msg)
if status:
print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
print "No word from block list is presnt in input '%s'"%(msg)
msg = "True test case ? normal damn.,"
status, bloak_char = getbBlockListFlag(msg)
if status:
print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
print "No word from block list is presnt in input '%s'"%(msg)
msg = "False test case ? nothing when sequance aaammdddnnn.."
status, bloak_char = getbBlockListFlag(msg)
if status:
print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
print "No word from block list is presnt in input '%s'"%(msg)
msg = "False test case ? nothing.."
status, bloak_char = getbBlockListFlag(msg)
if status:
print "The word `%s` from block list is presnt in input '%s'"%(''.join(bloak_char), msg)
else:
print "No word from block list is presnt in input '%s'"%(msg)
msg = "True test case ? Handle daaaammaaaammnnnnn.,"
status, block_char = getbBlockListFlag(msg)
if status:
print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
print "No word from block list is presnt in input '%s'"%(msg)
Output:
vivek@vivek:~/Desktop/stackoverflow$ python 16.py
The word `damn` from block list is presnt in input 'True test case ? daaaammmmnnnnn.,'
The word `damn` from block list is presnt in input 'True test case ? normal damn.,'
No word from block list is presnt in input 'False test case ? nothing when sequance aaammdddnnn..'
No word from block list is presnt in input 'False test case ? nothing..'
No word from block list is presnt in input 'True test case ? Handle daaaammaaaammnnnnn.,'