Python - 匹配两个单独数组中的闭合字符串

Python - Matching close strings in two separate arrays

我有两个不同的数组,其中填充了一些字符串值,我想比较这两个数组并找到匹配项。主要问题是我试图找到匹配项的两个字符串并不完全相同,并且可能包含诸如‘-‘

之类的符号

例如我想匹配:

‘猫老鼠’的单词是猫

我试过使用 diff lib close_matches 库,但没有找到匹配项。

我也尝试过使用交集法来比较两组字母,但由于字符串不完全相同,这会导致问题。

还有其他方法可以尝试吗?

如果您只需要知道 word/substring 是否在字符串中,那么您不需要关心该词周围的所有其他内容。 但是你需要注意,你的例子是区分大小写的。

两种方式:

1)

s = "Cat-Dog"
search = "cat"

result = search in s.lower() # s.lower equals "cat-dog"
print(result)
True
  1. 使用正则表达式
import re

s = "Cat-Dog"
search = "cat"

if re.search(search, s, re.IGNORECASE): #True when there is a match
   print('found a match')