我有两个字符串列表,一个叫l1
另一个l2
。我有兴趣为 中的每个字符串找到l1
最匹配的字符串l2
(但不是相反,即我只关心 中的字符串l1
)。我知道没有完美的匹配。我使用 jaro-winkler 分数来计算每个字符串的相似度,使用 jellyfish 模块。
为了做到这一点,我创建了一个包含所有 jaro-winkler 分数的矩阵,然后找到 maxtrix 中每个点的最大值。但是,问题是有时字符串 froml2
可能是多个字符串 from 的最佳匹配,l1
我想防止这种情况发生。
有没有办法优化 argmax 方法,使得最大索引位置只能出现在结果矩阵中一次?
举个例子,拖车列表和后续代码如下:
l1 = ['skinnycorebrokenblack184567', 'promtex2365h6', 'lovelinen940770', 'promtex2365h1', 'lovetrs844705',
'lovetrs844704', 'bennttrs49655', 'stella55900', 'kaxsprassel55250', 'smurfbs185573', 'kaxsprassel55880',
'victoriacort182062', 'juliatreggings916531', 'juliatreggings916530', 'milo63624505', 'promtex2365s2',
'promtex2365s1', 'promtex2365s6', 'promtex2365s4', 'stantwill160810', 'topazchini51081', 'topazchini51087',
'juliatreggings187109', 'hansentrs50924', '2454s1ladiesjeanscolure', 'promtex2365h2']
l2 = ['stannewtwill160810', 'stellatrs55900', 'jennyhigh352300', 'victoriacort180565',
'mistylowribsatins818820202031', 'lovelinen940771', 'kaxsprasseltrs55250', 'milo63626624', 'lovetrs844702',
'sarabootcuts842887019398270', 'sarabootcuts84288701939', 'victoriacords81805848817',
'ladiesjeanscolouredxxl2454s340999', 'julliatregging1871168817', 'logandrawstringpants92686705656',
'72480', 'victoriacords85203408817', 'julliatregging9673907817', 'lilypoplin9418412031', 'stellatrs56023',
'tysontrs50626', 'bolttrousers51370', 'bellamystripe184539', 'tenrhino63602214', 'kidsthermotrousers2365h1',
'bennytrouser53648', 'bluerinse070201072', 'topazchino51077', 'slimclassicblack674220203128999',
'milo63603812', 'milo63603813', 'milo63603814', 'slimclassicblack6742202031', 'lilypoplin9418402031',
'julliatregging9673917817', 'smurfjr185606', 'sarabootcuts81884571939', 'julliatregging9165318817']
#create the matrix
mat = np.matrix([[jf.jaro_distance(str(st1), str(st2))
if jf.jaro_distance(str(st1), str(st2)) > 0.85 else 0
for st2 in l2] for st1 in l1])
#get max values
mat_max = (mat.argmax(1))
#create match dictionary
match_dict = {}
for x in xrange(len(mat_max)):
if int(mat_max[x]):
match_dict[styles[x]] = s2[int(mat_max[x])]
在上面的示例中,请注意'topazchino51077'
from与 froml2
的字符串匹配两次l1
。这正是我希望防止的。来自 l2 的字符串应该与最佳匹配进行匹配。