{"id":934525,"date":"2024-12-26T18:37:05","date_gmt":"2024-12-26T10:37:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/934525.html"},"modified":"2024-12-26T18:37:07","modified_gmt":"2024-12-26T10:37:07","slug":"python%e5%a6%82%e4%bd%95%e6%90%9c%e7%b4%a2%e9%a2%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/934525.html","title":{"rendered":"python\u5982\u4f55\u641c\u7d22\u9898"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071502\/2c2a4fa8-57ee-4eaa-b595-b5c2b9bab309.webp\" alt=\"python\u5982\u4f55\u641c\u7d22\u9898\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8fdb\u884c\u641c\u7d22\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u7b2c\u4e09\u65b9\u5e93\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u8fdb\u884c\u7b80\u5355\u641c\u7d22\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u590d\u6742\u641c\u7d22\u3001\u4f7f\u7528\u641c\u7d22\u7b97\u6cd5\uff08\u5982\u4e8c\u5206\u641c\u7d22\uff09\u4f18\u5316\u6027\u80fd\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u8fd9\u4e9b\u641c\u7d22\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u4e2d\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u7528\u4e8e\u641c\u7d22\u548c\u5339\u914d\uff0c\u5176\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u590d\u6742\u641c\u7d22\u4efb\u52a1\u7684\u5f3a\u5927\u5de5\u5177\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684<code>re<\/code>\u6a21\u5757\u4f7f\u7528\uff0c\u5b83\u5141\u8bb8\u4f60\u5b9a\u4e49\u641c\u7d22\u6a21\u5f0f\u5e76\u5728\u6587\u672c\u4e2d\u67e5\u627e\u5339\u914d\u9879\u3002\u6b64\u5916\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u5982<code>find()<\/code>\u548c<code>index()<\/code>\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u641c\u7d22\u3002\u5bf9\u4e8e\u9700\u8981\u9ad8\u6548\u641c\u7d22\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u5229\u7528\u6570\u636e\u7ed3\u6784\u5982\u5217\u8868\u3001\u5b57\u5178\u548c\u96c6\u5408\uff0c\u4ee5\u53ca\u7b97\u6cd5\u5982\u4e8c\u5206\u641c\u7d22\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u4e0b\u9762\u5c06\u9010\u4e00\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u8fdb\u884c\u7b80\u5355\u641c\u7d22<\/p>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u800c\u5b9e\u7528\u7684\u641c\u7d22\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u5339\u914d\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>find()<\/code>\u548c<code>index()<\/code><\/li>\n<\/ol>\n<p><p><code>find()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u5b50\u5b57\u7b26\u4e32\u7684\u9996\u6b21\u51fa\u73b0\uff0c\u5e76\u8fd4\u56de\u5176\u7d22\u5f15\u3002\u5982\u679c\u5b50\u5b57\u7b26\u4e32\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de-1\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c<code>index()<\/code>\u65b9\u6cd5\u4e0e<code>find()<\/code>\u76f8\u4f3c\uff0c\u4f46\u5728\u5b50\u5b57\u7b26\u4e32\u4e0d\u5b58\u5728\u65f6\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Welcome to Python programming&quot;<\/p>\n<p>position = text.find(&quot;Python&quot;)<\/p>\n<p>print(position)  # \u8f93\u51fa\uff1a11<\/p>\n<h2><strong>\u4f7f\u7528index()\u65b9\u6cd5<\/strong><\/h2>\n<p>try:<\/p>\n<p>    position_index = text.index(&quot;Python&quot;)<\/p>\n<p>    print(position_index)  # \u8f93\u51fa\uff1a11<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;Substring not found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57<\/li>\n<\/ol>\n<p><p><code>in<\/code>\u5173\u952e\u5b57\u662f\u4e00\u79cd\u7b80\u5355\u4e14\u76f4\u89c2\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u68c0\u67e5\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u5b58\u5728\u4e8e\u5b57\u7b26\u4e32\u4e2d\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u5b50\u5b57\u7b26\u4e32\u662f\u5426\u88ab\u627e\u5230\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if &quot;Python&quot; in text:<\/p>\n<p>    print(&quot;Found &#39;Python&#39; in the text&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;&#39;Python&#39; not found in the text&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u590d\u6742\u641c\u7d22<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u80fd\u591f\u8fdb\u884c\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u548c\u641c\u7d22\u4efb\u52a1\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u57fa\u672c\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u4f7f\u7528\u5305\u62ec\u5339\u914d\u3001\u641c\u7d22\u548c\u66ff\u6362\u6587\u672c\u3002<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>match()<\/code>\u3001<code>search()<\/code>\u548c<code>findall()<\/code>\u7b49\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = r&quot;\\bPython\\b&quot;<\/p>\n<p>text = &quot;I am learning Python programming.&quot;<\/p>\n<h2><strong>\u4f7f\u7528search()\u65b9\u6cd5<\/strong><\/h2>\n<p>result = re.search(pattern, text)<\/p>\n<p>if result:<\/p>\n<p>    print(&quot;Match found:&quot;, result.group())<\/p>\n<p>else:<\/p>\n<p>    print(&quot;No match found&quot;)<\/p>\n<h2><strong>\u4f7f\u7528findall()\u65b9\u6cd5<\/strong><\/h2>\n<p>matches = re.findall(pattern, text)<\/p>\n<p>print(&quot;Matches found:&quot;, matches)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u66ff\u6362<\/li>\n<\/ol>\n<p><p><code>re.sub()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5339\u914d\u7684\u6587\u672c\u66ff\u6362\u4e3a\u6307\u5b9a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python is awesome. Python is versatile.&quot;<\/p>\n<p>new_text = re.sub(r&quot;Python&quot;, &quot;Programming&quot;, text)<\/p>\n<p>print(new_text)  # \u8f93\u51fa\uff1aProgramming is awesome. Programming is versatile.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u641c\u7d22\u7b97\u6cd5\u4f18\u5316\u6027\u80fd<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u9ad8\u6548\u641c\u7d22\u7684\u573a\u666f\uff0c\u5c24\u5176\u662f\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0c\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4e8c\u5206\u641c\u7d22<\/li>\n<\/ol>\n<p><p>\u4e8c\u5206\u641c\u7d22\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u641c\u7d22\u7b97\u6cd5\uff0c\u9002\u7528\u4e8e\u5df2\u6392\u5e8f\u7684\u5e8f\u5217\u3002\u5b83\u901a\u8fc7\u4e0d\u65ad\u5c06\u641c\u7d22\u8303\u56f4\u7f29\u5c0f\u4e00\u534a\u6765\u67e5\u627e\u76ee\u6807\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def binary_search(arr, target):<\/p>\n<p>    left, right = 0, len(arr) - 1<\/p>\n<p>    while left &lt;= right:<\/p>\n<p>        mid = left + (right - left) \/\/ 2<\/p>\n<p>        if arr[mid] == target:<\/p>\n<p>            return mid<\/p>\n<p>        elif arr[mid] &lt; target:<\/p>\n<p>            left = mid + 1<\/p>\n<p>        else:<\/p>\n<p>            right = mid - 1<\/p>\n<p>    return -1<\/p>\n<p>sorted_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>index = binary_search(sorted_list, 5)<\/p>\n<p>print(index)  # \u8f93\u51fa\uff1a4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u96c6\u5408\u548c\u5b57\u5178<\/li>\n<\/ol>\n<p><p>\u96c6\u5408\u548c\u5b57\u5178\u5728Python\u4e2d\u4f7f\u7528\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u63d0\u4f9b\u4e86\u5e38\u6570\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u67e5\u627e\u6027\u80fd\uff0c\u9002\u5408\u7528\u4e8e\u5feb\u901f\u67e5\u627e\u548c\u53bb\u91cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u96c6\u5408\u53bb\u91cd<\/p>\n<p>items = [&quot;apple&quot;, &quot;banana&quot;, &quot;apple&quot;, &quot;orange&quot;]<\/p>\n<p>unique_items = set(items)<\/p>\n<p>print(unique_items)  # \u8f93\u51fa\uff1a{&#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;}<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u5feb\u901f\u67e5\u627e<\/strong><\/h2>\n<p>phonebook = {&quot;Alice&quot;: &quot;123-4567&quot;, &quot;Bob&quot;: &quot;987-6543&quot;}<\/p>\n<p>number = phonebook.get(&quot;Alice&quot;)<\/p>\n<p>print(number)  # \u8f93\u51fa\uff1a123-4567<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u8fdb\u884c\u9ad8\u7ea7\u641c\u7d22<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u80fd\u591f\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u641c\u7d22\u4efb\u52a1\uff0c\u5982\u5168\u6587\u641c\u7d22\u548c\u6a21\u5f0f\u5339\u914d\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5168\u6587\u641c\u7d22\u4e0e\u5012\u6392\u7d22\u5f15<\/li>\n<\/ol>\n<p><p>\u5168\u6587\u641c\u7d22\u901a\u5e38\u7528\u4e8e\u641c\u7d22\u5f15\u64ce\uff0c\u901a\u8fc7\u6784\u5efa\u5012\u6392\u7d22\u5f15\u52a0\u5feb\u641c\u7d22\u901f\u5ea6\u3002\u5012\u6392\u7d22\u5f15\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8\u6587\u6863\u4e2d\u8bcd\u6c47\u7684\u6620\u5c04\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u7b80\u5355\u7684\u5012\u6392\u7d22\u5f15\u5b9e\u73b0<\/p>\n<p>from collections import defaultdict<\/p>\n<p>def build_inverted_index(documents):<\/p>\n<p>    inverted_index = defaultdict(set)<\/p>\n<p>    for doc_id, text in enumerate(documents):<\/p>\n<p>        for word in text.split():<\/p>\n<p>            inverted_index[word].add(doc_id)<\/p>\n<p>    return inverted_index<\/p>\n<p>documents = [<\/p>\n<p>    &quot;Python is a programming language&quot;,<\/p>\n<p>    &quot;Python is popular&quot;,<\/p>\n<p>    &quot;Data science uses Python&quot;<\/p>\n<p>]<\/p>\n<p>index = build_inverted_index(documents)<\/p>\n<p>print(index[&quot;Python&quot;])  # \u8f93\u51fa\uff1a{0, 1, 2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Trie\u6811\u8fdb\u884c\u524d\u7f00\u641c\u7d22<\/li>\n<\/ol>\n<p><p>Trie\u6811\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u524d\u7f00\u641c\u7d22\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u81ea\u52a8\u8865\u5168\u548c\u62fc\u5199\u68c0\u67e5\u7b49\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class TrieNode:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.children = {}<\/p>\n<p>        self.is_end_of_word = False<\/p>\n<p>class Trie:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.root = TrieNode()<\/p>\n<p>    def insert(self, word):<\/p>\n<p>        node = self.root<\/p>\n<p>        for char in word:<\/p>\n<p>            if char not in node.children:<\/p>\n<p>                node.children[char] = TrieNode()<\/p>\n<p>            node = node.children[char]<\/p>\n<p>        node.is_end_of_word = True<\/p>\n<p>    def search(self, word):<\/p>\n<p>        node = self.root<\/p>\n<p>        for char in word:<\/p>\n<p>            if char not in node.children:<\/p>\n<p>                return False<\/p>\n<p>            node = node.children[char]<\/p>\n<p>        return node.is_end_of_word<\/p>\n<p>    def starts_with(self, prefix):<\/p>\n<p>        node = self.root<\/p>\n<p>        for char in prefix:<\/p>\n<p>            if char not in node.children:<\/p>\n<p>                return False<\/p>\n<p>            node = node.children[char]<\/p>\n<p>        return True<\/p>\n<p>trie = Trie()<\/p>\n<p>trie.insert(&quot;apple&quot;)<\/p>\n<p>trie.insert(&quot;app&quot;)<\/p>\n<p>print(trie.search(&quot;apple&quot;))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p>print(trie.starts_with(&quot;ap&quot;))  # \u8f93\u51fa\uff1aTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6027\u80fd\u4f18\u5316\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u548c\u5b9e\u65bd\u641c\u7d22\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u8003\u8651\u56e0\u7d20\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/li>\n<\/ol>\n<p><p>\u4e0d\u540c\u6570\u636e\u7ed3\u6784\u5728\u65f6\u95f4\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e0a\u8868\u73b0\u4e0d\u540c\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u63d0\u5347\u641c\u7d22\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u96c6\u5408\u548c\u5b57\u5178\u8fdb\u884c\u5feb\u901f\u67e5\u627e\uff0c\u4f7f\u7528\u5217\u8868\u8fdb\u884c\u6709\u5e8f\u6570\u636e\u7684\u5b58\u50a8\u7b49\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8003\u8651\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u73b0\u641c\u7d22\u7b97\u6cd5\u65f6\uff0c\u9700\u8003\u8651\u5176\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u9009\u62e9\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u4f4e\u7684\u7b97\u6cd5\uff08\u5982\u4e8c\u5206\u641c\u7d22\uff09\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u5e76\u884c\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u641c\u7d22\u4efb\u52a1\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6280\u672f\u8fdb\u884c\u5e76\u884c\u5904\u7406\uff0c\u4ee5\u63d0\u5347\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u641c\u7d22\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u4ece\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u5230\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u9ad8\u7ea7\u641c\u7d22\u7b97\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u6848\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u4f7f\u7528\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u66f4\u9ad8\u6548\u548c\u7075\u6d3b\u7684\u641c\u7d22\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u5b57\u7b26\u4e32\u641c\u7d22\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u5982<code>find()<\/code>\u3001<code>index()<\/code>\u548c<code>count()<\/code>\u6765\u641c\u7d22\u5b57\u7b26\u4e32\u3002<code>find()<\/code>\u65b9\u6cd5\u4f1a\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u9996\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\uff0c\u5982\u679c\u672a\u627e\u5230\u5219\u8fd4\u56de-1\uff1b<code>index()<\/code>\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5982\u679c\u672a\u627e\u5230\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\uff1b<code>count()<\/code>\u65b9\u6cd5\u5219\u4f1a\u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u5728\u4e3b\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u6b64\u5916\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<code>re<\/code>\u4e5f\u63d0\u4f9b\u4e86\u66f4\u590d\u6742\u7684\u641c\u7d22\u529f\u80fd\uff0c\u53ef\u4ee5\u5339\u914d\u6a21\u5f0f\u548c\u8fdb\u884c\u66f4\u7075\u6d3b\u7684\u641c\u7d22\u3002<\/p>\n<p><strong>Python\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u8fdb\u884c\u6587\u672c\u641c\u7d22\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u6587\u672c\u641c\u7d22\uff0c\u6bd4\u5982<code>re<\/code>\u5e93\u7528\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff0c<code>pandas<\/code>\u5e93\u53ef\u4ee5\u5904\u7406\u6570\u636e\u6846\u4e2d\u7684\u6587\u672c\u641c\u7d22\uff0c<code>Whoosh<\/code>\u548c<code>Elasticsearch<\/code>\u5219\u9002\u5408\u7528\u4e8e\u6784\u5efa\u641c\u7d22\u5f15\u64ce\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u9ad8\u6548\u5730\u67e5\u627e\u548c\u5904\u7406\u6587\u672c\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u6587\u4ef6\u5185\u5bb9\u7684\u641c\u7d22\uff1f<\/strong><br \/>\u8981\u5728\u6587\u4ef6\u4e2d\u641c\u7d22\u7279\u5b9a\u5185\u5bb9\uff0c\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u5e76\u9010\u884c\u8bfb\u53d6\u3002\u7ed3\u5408\u5b57\u7b26\u4e32\u641c\u7d22\u65b9\u6cd5\uff0c\u5982<code>in<\/code>\u5173\u952e\u5b57\u6216<code>find()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u67e5\u627e\u76ee\u6807\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>with open(&#39;filename.txt&#39;) as file:<\/code>\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528\u5faa\u73af\u9010\u884c\u68c0\u67e5\u5185\u5bb9\u3002\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5f02\u5e38\u5904\u7406\uff0c\u786e\u4fdd\u5728\u6587\u4ef6\u672a\u627e\u5230\u65f6\u4e0d\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fdb\u884c\u641c\u7d22\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u7b2c\u4e09\u65b9\u5e93\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":934526,"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\/934525"}],"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=934525"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/934525\/revisions"}],"predecessor-version":[{"id":934527,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/934525\/revisions\/934527"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/934526"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=934525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=934525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=934525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}