欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 编程语言 > >内容正文

python

python列表查找相同字符串-尊龙游戏旗舰厅官网

发布时间:2025/1/21 20 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 python列表查找相同字符串_从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列表中查找输入字符串的所有紧密匹配项的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图