我正在检查给定的句子中是否存在密钥
但是在句子中,键可以是混乱的,或者在它们之间可以有特殊字符,或者键之间可以有一些单词。 现在我不担心区分大小写
例如,我想检查一下:
我从一个字符一个字符地比较键和语句开始,但我没能解决这个问题
基于这些评论,我很想写代码
def getMatch(text,key):
matchedwords=list()
i=0
for j in range(0, len(text)):
for i in range(0,len(key)):
match = ""
keyindex=i
textindex=j
while(keyindex<len(key) and textindex<len(text) and key[keyindex]==text[textindex] ):
match+=text[textindex]
keyindex+=1
textindex+=1
if(len(match)>0):
if(match not in matchedwords):
matchedwords.append(match)
print(matchedwords)
text="MOTOR Device 101 is high"
key="MOTOR101"
getMatch(text,key)
我能够得到输出为“['MOTOR'、'otr'、'O'、'TOR'、'OR'、'R'、'101'、'1'、'01']”。 请让我知道,如果需要任何更改或改进可以做。 从这里,我在这里试图检查是否有任何单词组合导致“MOTOR101”
您正在字符串中搜索两个单独的项。如果将它们拆分为一个列表,那么下面的代码将找到它们
此代码将搜索并查找两个以上的项目
好的,我发现单词的重复会导致误报,所以。。。 这是我的第三次尝试
您需要分别搜索
Motor
和101
,然后返回状态(如果两者都存在或不存在)相关问题 更多 >
编程相关推荐