如何用Python实现字符串的模糊匹配算法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计351个文字,预计阅读时间需要2分钟。
目录Python 字符串模糊匹配 + 包含四个参数 + python-re 模块,模糊匹配 + Python 字符串模糊匹配 + Python 的 difflib 库中 get_close_matches 方法 + 包含四个参数 + x +:被匹配的字符串words:去匹配的字符串列表
目录
- Python字符串模糊匹配
- 包含四个参数
- python-re模块,模糊匹配
Python字符串模糊匹配
Python的difflib库中get_close_matches方法
包含四个参数
x:被匹配的字符串。words:去匹配的字符串列表。n,前topn个最佳匹配返回,默认为3。cutoff:匹配度大小,为[0, 1]浮点数,默认数值0.6。
import difflib list1 = ['ape', 'apple', 'peach', 'puppy'] difflib.get_close_matches('appel', list1)
import keyword difflib.get_close_matches('wheel', keyword.kwlist)
difflib.get_close_matches('pineapple', keyword.kwlist)
difflib.get_close_matches('accept', keyword.kwlist)
利用这个功能就能够实现SQL中的LIKE模糊查询。
python-re模块,模糊匹配
import re def fuzzyMatch(): value = '西西' list = ['大海西西的', '大家西西', '打架', '西都好快', '西西大化'] pattern = '.*' + value + '.*' for s in list: obj = re.findall(pattern, s) if len(obj) > 0: a = s print(a) break fuzzyMatch()
以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。
本文共计351个文字,预计阅读时间需要2分钟。
目录Python 字符串模糊匹配 + 包含四个参数 + python-re 模块,模糊匹配 + Python 字符串模糊匹配 + Python 的 difflib 库中 get_close_matches 方法 + 包含四个参数 + x +:被匹配的字符串words:去匹配的字符串列表
目录
- Python字符串模糊匹配
- 包含四个参数
- python-re模块,模糊匹配
Python字符串模糊匹配
Python的difflib库中get_close_matches方法
包含四个参数
x:被匹配的字符串。words:去匹配的字符串列表。n,前topn个最佳匹配返回,默认为3。cutoff:匹配度大小,为[0, 1]浮点数,默认数值0.6。
import difflib list1 = ['ape', 'apple', 'peach', 'puppy'] difflib.get_close_matches('appel', list1)
import keyword difflib.get_close_matches('wheel', keyword.kwlist)
difflib.get_close_matches('pineapple', keyword.kwlist)
difflib.get_close_matches('accept', keyword.kwlist)
利用这个功能就能够实现SQL中的LIKE模糊查询。
python-re模块,模糊匹配
import re def fuzzyMatch(): value = '西西' list = ['大海西西的', '大家西西', '打架', '西都好快', '西西大化'] pattern = '.*' + value + '.*' for s in list: obj = re.findall(pattern, s) if len(obj) > 0: a = s print(a) break fuzzyMatch()
以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

