{"id":1138846,"date":"2025-01-08T22:04:16","date_gmt":"2025-01-08T14:04:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1138846.html"},"modified":"2025-01-08T22:04:18","modified_gmt":"2025-01-08T14:04:18","slug":"python%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a6%82%e4%bd%95%e5%92%8c%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%a8%a1%e7%b3%8a%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1138846.html","title":{"rendered":"python\u5b57\u7b26\u4e32\u5982\u4f55\u548c\u5b57\u7b26\u4e32\u6a21\u7cca\u5339\u914d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102323\/95e6333e-a516-4cc3-a1bd-a34b33c0a4a0.webp\" alt=\"python\u5b57\u7b26\u4e32\u5982\u4f55\u548c\u5b57\u7b26\u4e32\u6a21\u7cca\u5339\u914d\" \/><\/p>\n<p><p> <strong>Python \u5b57\u7b26\u4e32\u5982\u4f55\u548c\u5b57\u7b26\u4e32\u6a21\u7cca\u5339\u914d\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001Levenshtein \u8ddd\u79bb\u3001\u6a21\u7cca\u5b57\u7b26\u4e32\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u6a21\u7cca\u5339\u914d\uff0c\u5305\u62ec<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u3001Levenshtein \u8ddd\u79bb\u548c\u6a21\u7cca\u5b57\u7b26\u4e32\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expressions, \u7b80\u79f0 Regex\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u7b26\u5408\u7279\u5b9a\u6a21\u5f0f\u7684\u5b50\u5b57\u7b26\u4e32\u3002Levenshtein \u8ddd\u79bb\u5219\u662f\u4e00\u79cd\u8ba1\u7b97\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u6700\u5c0f\u7f16\u8f91\u8ddd\u79bb\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u8bc4\u4f30\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u76f8\u4f3c\u5ea6\u3002\u800c\u6a21\u7cca\u5b57\u7b26\u4e32\u5e93\uff08\u5982 FuzzyWuzzy\uff09\u5219\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7684\u529f\u80fd\uff0c\u7528\u4e8e\u66f4\u7b80\u5355\u5730\u5b9e\u73b0\u6a21\u7cca\u5339\u914d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u6a21\u5f0f\u7684\u5de5\u5177\u3002Python \u63d0\u4f9b\u4e86 <code>re<\/code> \u6a21\u5757\uff0c\u4f7f\u5f97\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528\u53d8\u5f97\u7b80\u5355\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u5339\u914d\u56fa\u5b9a\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5339\u914d\u5305\u542b\u67d0\u4e2a\u5355\u8bcd\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&#39;hello&#39;<\/p>\n<p>string = &#39;hello world&#39;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;Match found:&quot;, match.group())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No match found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6a21\u7cca\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u3002\u4f8b\u5982\uff0c\u5339\u914d\u4ee5\u201che\u201d\u5f00\u5934\uff0c\u4ee5\u201co\u201d\u7ed3\u5c3e\uff0c\u4e2d\u95f4\u6709\u4efb\u610f\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pattern = r&#39;he.*o&#39;<\/p>\n<p>string = &#39;hello world&#39;<\/p>\n<p>match = re.search(pattern, string)<\/p>\n<p>if match:<\/p>\n<p>    print(&quot;Match found:&quot;, match.group())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No match found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001Levenshtein \u8ddd\u79bb<\/h3>\n<\/p>\n<p><p>Levenshtein \u8ddd\u79bb\uff0c\u4e5f\u79f0\u4e3a\u7f16\u8f91\u8ddd\u79bb\uff0c\u662f\u4e00\u79cd\u8861\u91cf\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u5dee\u5f02\u7684\u5ea6\u91cf\u65b9\u6cd5\u3002\u5b83\u8ba1\u7b97\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u6240\u9700\u7684\u6700\u5c11\u7f16\u8f91\u64cd\u4f5c\u6b21\u6570\uff08\u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\uff09\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u8ba1\u7b97 Levenshtein \u8ddd\u79bb<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>python-Levenshtein<\/code> \u5e93\u6765\u8ba1\u7b97\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684 Levenshtein \u8ddd\u79bb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import Levenshtein<\/p>\n<p>string1 = &#39;hello&#39;<\/p>\n<p>string2 = &#39;hallo&#39;<\/p>\n<p>distance = Levenshtein.distance(string1, string2)<\/p>\n<p>print(&quot;Levenshtein distance:&quot;, distance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528 Levenshtein \u8ddd\u79bb\u8fdb\u884c\u6a21\u7cca\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684 Levenshtein \u8ddd\u79bb\uff0c\u53ef\u4ee5\u5224\u65ad\u5b83\u4eec\u7684\u76f8\u4f3c\u5ea6\u3002\u5982\u679c\u8ddd\u79bb\u8f83\u5c0f\uff0c\u5219\u8ba4\u4e3a\u5b83\u4eec\u662f\u76f8\u4f3c\u7684\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">threshold = 2<\/p>\n<p>if distance &lt;= threshold:<\/p>\n<p>    print(&quot;Strings are similar&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Strings are not similar&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6a21\u7cca\u5b57\u7b26\u4e32\u5e93\uff08FuzzyWuzzy\uff09<\/h3>\n<\/p>\n<p><p>FuzzyWuzzy \u662f\u4e00\u4e2a\u7528\u4e8e\u5b57\u7b26\u4e32\u6a21\u7cca\u5339\u914d\u7684 Python \u5e93\uff0c\u5b83\u57fa\u4e8e Levenshtein \u8ddd\u79bb\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f7f\u5f97\u6a21\u7cca\u5339\u914d\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7 pip \u5b89\u88c5 FuzzyWuzzy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install fuzzywuzzy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8fdb\u884c\u5b57\u7b26\u4e32\u5339\u914d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fuzzywuzzy import fuzz<\/p>\n<p>string1 = &#39;hello&#39;<\/p>\n<p>string2 = &#39;hallo&#39;<\/p>\n<p>ratio = fuzz.ratio(string1, string2)<\/p>\n<p>print(&quot;Similarity ratio:&quot;, ratio)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528 FuzzyWuzzy \u8fdb\u884c\u6a21\u7cca\u5339\u914d<\/h4>\n<\/p>\n<p><p>FuzzyWuzzy \u63d0\u4f9b\u4e86\u591a\u79cd\u5339\u914d\u65b9\u6cd5\uff0c\u4f8b\u5982 <code>partial_ratio<\/code>\u3001<code>token_sort_ratio<\/code> \u548c <code>token_set_ratio<\/code>\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fuzzywuzzy import process<\/p>\n<p>choices = [&#39;hello world&#39;, &#39;hi there&#39;, &#39;hello&#39;]<\/p>\n<p>query = &#39;helo&#39;<\/p>\n<p>best_match = process.extractOne(query, choices)<\/p>\n<p>print(&quot;Best match:&quot;, best_match)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u4e0a\u8ff0\u591a\u79cd\u65b9\u6cd5\uff0c\u4ee5\u83b7\u5f97\u66f4\u9ad8\u7684\u5339\u914d\u7cbe\u5ea6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u521d\u6b65\u7b5b\u9009\uff0c\u7136\u540e\u4f7f\u7528 Levenshtein \u8ddd\u79bb\u6216 FuzzyWuzzy \u8fdb\u884c\u7cbe\u786e\u5339\u914d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>import Levenshtein<\/p>\n<p>from fuzzywuzzy import process<\/p>\n<h2><strong>\u521d\u6b65\u7b5b\u9009<\/strong><\/h2>\n<p>pattern = r&#39;he.*o&#39;<\/p>\n<p>choices = [&#39;hello world&#39;, &#39;hi there&#39;, &#39;hello&#39;]<\/p>\n<p>filtered_choices = [s for s in choices if re.search(pattern, s)]<\/p>\n<h2><strong>\u7cbe\u786e\u5339\u914d<\/strong><\/h2>\n<p>query = &#39;helo&#39;<\/p>\n<p>best_match = None<\/p>\n<p>best_distance = float(&#39;inf&#39;)<\/p>\n<p>for choice in filtered_choices:<\/p>\n<p>    distance = Levenshtein.distance(query, choice)<\/p>\n<p>    if distance &lt; best_distance:<\/p>\n<p>        best_distance = distance<\/p>\n<p>        best_match = choice<\/p>\n<p>print(&quot;Best match:&quot;, best_match)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u6a21\u7cca\u5339\u914d\u5728\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>5.1 \u641c\u7d22\u5f15\u64ce<\/h4>\n<\/p>\n<p><p>\u5728\u641c\u7d22\u5f15\u64ce\u4e2d\uff0c\u901a\u8fc7\u6a21\u7cca\u5339\u914d\u53ef\u4ee5\u63d0\u9ad8\u7528\u6237\u641c\u7d22\u4f53\u9a8c\u3002\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u8f93\u5165\u62fc\u5199\u9519\u8bef\u7684\u5173\u952e\u8bcd\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u7cca\u5339\u914d\u627e\u5230\u6700\u63a5\u8fd1\u7684\u6b63\u786e\u5173\u952e\u8bcd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fuzzywuzzy import process<\/p>\n<p>documents = [&#39;machine learning&#39;, &#39;deep learning&#39;, &#39;artificial intelligence&#39;]<\/p>\n<p>query = &#39;machne learning&#39;<\/p>\n<p>best_match = process.extractOne(query, documents)<\/p>\n<p>print(&quot;Best match:&quot;, best_match)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6e05\u6d17\u8fc7\u7a0b\u4e2d\uff0c\u6a21\u7cca\u5339\u914d\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u548c\u5408\u5e76\u76f8\u4f3c\u7684\u8bb0\u5f55\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u5305\u542b\u5ba2\u6237\u59d3\u540d\u7684\u6570\u636e\u5e93\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u7cca\u5339\u914d\u8bc6\u522b\u548c\u5408\u5e76\u62fc\u5199\u9519\u8bef\u7684\u59d3\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fuzzywuzzy import process<\/p>\n<p>names = [&#39;John Doe&#39;, &#39;Jonn Doe&#39;, &#39;Johnny Do&#39;]<\/p>\n<p>query = &#39;John Doe&#39;<\/p>\n<p>matches = process.extract(query, names, limit=2)<\/p>\n<p>print(&quot;Matches:&quot;, matches)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.3 \u6587\u672c\u5206\u7c7b<\/h4>\n<\/p>\n<p><p>\u5728\u6587\u672c\u5206\u7c7b\u4efb\u52a1\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u7cca\u5339\u914d\u5c06\u6587\u672c\u5f52\u7c7b\u5230\u6700\u76f8\u4f3c\u7684\u7c7b\u522b\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u5305\u542b\u4e0d\u540c\u7c7b\u522b\u7684\u6587\u6863\u96c6\u5408\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u7cca\u5339\u914d\u5c06\u65b0\u6587\u6863\u5f52\u7c7b\u5230\u6700\u76f8\u4f3c\u7684\u7c7b\u522b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fuzzywuzzy import process<\/p>\n<p>categories = [&#39;Sports&#39;, &#39;Technology&#39;, &#39;Health&#39;]<\/p>\n<p>new_document = &#39;A new health technology&#39;<\/p>\n<p>best_category = process.extractOne(new_document, categories)<\/p>\n<p>print(&quot;Best category:&quot;, best_category)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f18\u5316\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u6a21\u7cca\u5339\u914d\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<p><h4>6.1 \u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u6a21\u7cca\u5339\u914d\u8ba1\u7b97\u91cf\u8f83\u5927\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u4f18\u5316\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u9884\u5904\u7406\u6570\u636e<\/strong>\uff1a\u5728\u8fdb\u884c\u6a21\u7cca\u5339\u914d\u4e4b\u524d\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4f8b\u5982\u53bb\u9664\u505c\u7528\u8bcd\u3001\u8f6c\u6362\u4e3a\u5c0f\u5199\u7b49\u3002<\/li>\n<li><strong>\u7d22\u5f15\u6280\u672f<\/strong>\uff1a\u901a\u8fc7\u5efa\u7acb\u7d22\u5f15\uff0c\u53ef\u4ee5\u52a0\u5feb\u5339\u914d\u901f\u5ea6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5012\u6392\u7d22\u5f15\u8fdb\u884c\u5feb\u901f\u67e5\u627e\u3002<\/li>\n<li><strong>\u5e76\u884c\u8ba1\u7b97<\/strong>\uff1a\u901a\u8fc7\u5e76\u884c\u8ba1\u7b97\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5339\u914d\u901f\u5ea6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\u3002<\/li>\n<\/ol>\n<p><h4>6.2 \u7cbe\u5ea6\u8c03\u6574<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u8c03\u6574\u6a21\u7cca\u5339\u914d\u7684\u7cbe\u5ea6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574 Levenshtein \u8ddd\u79bb\u7684\u9608\u503c\u6216 FuzzyWuzzy \u7684\u5339\u914d\u6bd4\u7387\u6765\u63a7\u5236\u5339\u914d\u7684\u7cbe\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fuzzywuzzy import process<\/p>\n<p>choices = [&#39;hello world&#39;, &#39;hi there&#39;, &#39;hello&#39;]<\/p>\n<p>query = &#39;helo&#39;<\/p>\n<p>best_match = process.extractOne(query, choices, scorer=fuzz.partial_ratio, score_cutoff=80)<\/p>\n<p>print(&quot;Best match:&quot;, best_match)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.3 \u5904\u7406\u7279\u6b8a\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6a21\u7cca\u5339\u914d\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u5904\u7406\u7279\u6b8a\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def preprocess(string):<\/p>\n<p>    return re.sub(r&#39;[^a-zA-Z0-9\\s]&#39;, &#39;&#39;, string)<\/p>\n<p>string = &#39;hello@world!&#39;<\/p>\n<p>cleaned_string = preprocess(string)<\/p>\n<p>print(&quot;Cleaned string:&quot;, cleaned_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001Levenshtein \u8ddd\u79bb\u548c\u6a21\u7cca\u5b57\u7b26\u4e32\u5e93\uff08\u5982 FuzzyWuzzy\uff09\uff0c\u53ef\u4ee5\u5728 Python \u4e2d\u5b9e\u73b0\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u6a21\u7cca\u5339\u914d\u529f\u80fd\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u63d0\u9ad8\u5339\u914d\u7cbe\u5ea6\u3002\u540c\u65f6\uff0c\u5728\u4f7f\u7528\u6a21\u7cca\u5339\u914d\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6027\u80fd\u4f18\u5316\u548c\u7cbe\u5ea6\u8c03\u6574\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u641c\u7d22\u5f15\u64ce\u3001\u6570\u636e\u6e05\u6d17\u8fd8\u662f\u6587\u672c\u5206\u7c7b\uff0c\u6a21\u7cca\u5339\u914d\u90fd\u80fd\u663e\u8457\u63d0\u9ad8\u7cfb\u7edf\u7684\u667a\u80fd\u5316\u548c\u7528\u6237\u4f53\u9a8c\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u672f\uff0c\u5c06\u4e3a\u60a8\u7684\u9879\u76ee\u5e26\u6765\u66f4\u591a\u53ef\u80fd\u6027\u548c\u66f4\u597d\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u6a21\u7cca\u5339\u914d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>fuzzywuzzy<\/code>\u5e93\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u6a21\u7cca\u5339\u914d\u3002\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6bd4\u8f83\u5b57\u7b26\u4e32\u7684\u76f8\u4f3c\u5ea6\uff0c\u5982<code>fuzz.ratio()<\/code>\u548c<code>fuzz.partial_ratio()<\/code>\u3002\u5b89\u88c5\u8be5\u5e93\u540e\uff0c\u53ef\u4ee5\u8f7b\u677e\u5bf9\u4e24\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u76f8\u4f3c\u5ea6\u8bc4\u5206\uff0c\u4ee5\u4fbf\u5728\u5904\u7406\u4e0d\u5b8c\u5168\u5339\u914d\u65f6\u4f7f\u7528\u3002<\/p>\n<p><strong>\u6a21\u7cca\u5339\u914d\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u6a21\u7cca\u5339\u914d\u5728\u8bb8\u591a\u573a\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u6bd4\u5982\u6587\u672c\u7ea0\u9519\u3001\u641c\u7d22\u5f15\u64ce\u4f18\u5316\u3001\u81ea\u7136\u8bed\u8a00\u5904\u7406\u3001\u6570\u636e\u6e05\u6d17\u7b49\u3002\u901a\u8fc7\u6a21\u7cca\u5339\u914d\uff0c\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u627e\u5230\u4e0e\u67e5\u8be2\u76f8\u4f3c\u4f46\u4e0d\u5b8c\u5168\u4e00\u81f4\u7684\u7ed3\u679c\uff0c\u4ece\u800c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u548c\u4fe1\u606f\u68c0\u7d22\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6a21\u7cca\u5339\u914d\u662f\u5426\u53ef\u884c\uff1f<\/strong><br \/>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u7684\u6a21\u7cca\u5339\u914d\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u6a21\u5f0f\u5339\u914d\u65f6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u6216\u5b57\u7b26\u7ec4\u5408\u7684\u5b57\u7b26\u4e32\u3002\u7136\u800c\uff0c\u4e0e<code>fuzzywuzzy<\/code>\u7b49\u4e13\u95e8\u7684\u6a21\u7cca\u5339\u914d\u5de5\u5177\u76f8\u6bd4\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7075\u6d3b\u6027\u548c\u529f\u80fd\u6027\u53ef\u80fd\u6709\u9650\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5b57\u7b26\u4e32\u5982\u4f55\u548c\u5b57\u7b26\u4e32\u6a21\u7cca\u5339\u914d\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001Levenshtein \u8ddd\u79bb\u3001\u6a21\u7cca\u5b57\u7b26\u4e32\u5e93 \u5728 Py [&hellip;]","protected":false},"author":3,"featured_media":1138851,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138846"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1138846"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138846\/revisions"}],"predecessor-version":[{"id":1138854,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1138846\/revisions\/1138854"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1138851"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1138846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1138846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1138846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}