网络编程
位置:首页>> 网络编程>> Python编程>> Python实现字符串模糊匹配方式

Python实现字符串模糊匹配方式

作者:Eureka丶  发布时间:2023-11-10 09:30:35 

标签:Python,字符串,模糊匹配

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)

Python实现字符串模糊匹配方式

import keyword

difflib.get_close_matches('wheel', keyword.kwlist)

Python实现字符串模糊匹配方式

difflib.get_close_matches('pineapple', keyword.kwlist)

Python实现字符串模糊匹配方式

difflib.get_close_matches('accept', keyword.kwlist)

Python实现字符串模糊匹配方式

利用这个功能就能够实现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()

来源:https://blog.csdn.net/Jeremiah_/article/details/121806739

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com