简体   繁体   English

比较List Similarity Python

[英]Compare List Similarity Python

S = ['hom']
L = ['home','honda','Hammer','Elephant']

I want to get output to show similarity 我想获得输出以显示相似性

hom = home 

and

print home

How I can do this? 我怎么能这样做? I want to use approximate matching to change "hom" to "home". 我想使用近似匹配将“hom”改为“home”。

For something built-in, you have difflib . 对于内置的东西,你有difflib

It can (as said in the docs): Return a list of the best “good enough” matches. 它可以(如文档中所述):返回最佳“足够好”的匹配列表。 word is a sequence for which close matches are desired (typically a string), and possibilities is a list of sequences against which to match word (typically a list of strings). word是需要密切匹配的序列(通常是字符串),并且可能性是与字匹配的序列列表(通常是字符串列表)。

The example in the docs is: 文档中的示例是:

>>> from difflib import get_close_matches
>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
['apple', 'ape']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM