from collections import OrderedDict
string = "test this string to check for sshh-iitit and dammm.mn"
new_string = string.replace('-','')
final_string = new_string.replace('.','')
words = final_string.split()
for word in words:
check = "".join(OrderedDict.fromkeys(word))
if check == "damn" or check == "shit":
print "true"
>>> 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
>>>
更新时间:
设置block列出单词。在
创建阻止列表单词字符列表。在
从输入字符串中删除标点符号。在
将字符串拆分为单词。在
拆分单词并从该单词创建字符序列列表。在
检查阻止列表字符列表中存在的字符序列列表。在
代码:
^{pr2}$
输出:
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.,'
你应该看看Levenshtein distance!PHP有一个很好的函数:leventstein()。对于Phython,至少有一个C实现:https://pypi.python.org/pypi/python-Levenshtein/0.12.0
当消息包含列表
block
中的任何项时,将flag
值设置为True
。在block
列表并通过for
循环迭代列表中的每个项。在flag
的值设置为False
,表示消息不包含任何阻止列表项。在block
列表中的项时,将flag
的值设置为True
。在break
语句停止for
循环迭代。在例如
更新时间:
block
列出单词。在代码:
^{pr2}$输出:
相关问题 更多 >
编程相关推荐