python列表查找相同字符串-尊龙游戏旗舰厅官网
假设我们给了一个单词,我们想找到它最接近的匹配项。不是完全匹配,而是其他单词在模式上与给定单词非常相似。为此,我们使用一个名为difflib的模块,并使用其名为get_close_matches的方法。
get_close_matches
此方法是difflib模块的一部分,为我们提供了我们指定的可能模式的匹配。下面是语法。difflib.get_close_matches(word, possibilities, n, cutoff)
word: it is the word to which we need to find the match.
possibilities: this is the patterns which will be compared for matching.
n: maximum number of close matches to return. should be greater than 0.
cutoff: the possibilities that do not score this float value between 0 and 1 are ignored.
运行上面的代码给我们以下结果-
示例
在下面的示例中,我们只说了一个单词,还列出了需要比较的可能性或模式的列表。然后我们应用该方法以获得所需的结果。from difflib import get_close_matches
word = 'banana'
patterns = ['ana', 'nana', 'ban', 'ran','tan']
print('matched words:',get_close_matches(word, patterns))
输出结果
运行上面的代码给我们以下结果-matched words: ['nana', 'ban', 'ana']
总结
以上是尊龙游戏旗舰厅官网为你收集整理的python列表查找相同字符串_从python列表中查找输入字符串的所有紧密匹配项的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: