{"id":1053363,"date":"2024-12-31T14:33:12","date_gmt":"2024-12-31T06:33:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1053363.html"},"modified":"2024-12-31T14:33:15","modified_gmt":"2024-12-31T06:33:15","slug":"python%e5%a6%82%e4%bd%95%e4%bb%8e%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%88%aa%e5%8f%96%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1053363.html","title":{"rendered":"python\u5982\u4f55\u4ece\u5b57\u7b26\u4e32\u622a\u53d6\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/e7926fd9-c75d-4430-9e15-5ec284558789.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4ece\u5b57\u7b26\u4e32\u622a\u53d6\u6574\u6570\" \/><\/p>\n<p><p> Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6574\u6570\u3002<strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5<\/strong>\u662f\u6700\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u5339\u914d\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u6a21\u5f0f\u7684\u5185\u5bb9\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u652f\u6301\u3002\u901a\u8fc7\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u4ece\u5b57\u7b26\u4e32\u4e2d\u8f7b\u677e\u63d0\u53d6\u51fa\u6240\u6709\u7684\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u8bb2\u89e3\u8fd9\u4e09\u79cd\u65b9\u6cd5\uff0c\u5e76\u5bf9\u5b83\u4eec\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5\u662f\u6700\u901a\u7528\u548c\u5f3a\u5927\u7684\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Python\u7684<code>re<\/code>\u6a21\u5757\u3002\u4f7f\u7528<code>re.findall<\/code>\u51fd\u6570\u53ef\u4ee5\u627e\u5230\u6240\u6709\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5b50\u5b57\u7b26\u4e32\u3002\u5bf9\u4e8e\u63d0\u53d6\u6574\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<code>\\d+<\/code>\uff0c\u5b83\u8868\u793a\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_integers(s):<\/p>\n<p>    return [int(num) for num in re.findall(r&#39;\\d+&#39;, s)]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;I have 2 apples and 10 bananas.&quot;<\/p>\n<p>print(extract_integers(s))  # \u8f93\u51fa: [2, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>re.findall(r&#39;\\d+&#39;, s)<\/code>\u627e\u5230\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d<code>\\d+<\/code>\u6a21\u5f0f\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5b57\u7b26\u4e32\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u63d0\u53d6\u6574\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u5b57\u7b26\u4e32\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u68c0\u67e5\u5b83\u662f\u5426\u662f\u6570\u5b57\uff0c\u5e76\u5c06\u5176\u8fde\u63a5\u8d77\u6765\u5f62\u6210\u6574\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u6ca1\u6709\u6b63\u5219\u8868\u8fbe\u5f0f\u90a3\u4e48\u7b80\u6d01\uff0c\u4f46\u4e5f\u5f88\u6709\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_integers(s):<\/p>\n<p>    integers = []<\/p>\n<p>    current_number = &quot;&quot;<\/p>\n<p>    for char in s:<\/p>\n<p>        if char.isdigit():<\/p>\n<p>            current_number += char<\/p>\n<p>        else:<\/p>\n<p>            if current_number:<\/p>\n<p>                integers.append(int(current_number))<\/p>\n<p>                current_number = &quot;&quot;<\/p>\n<p>    if current_number:<\/p>\n<p>        integers.append(int(current_number))<\/p>\n<p>    return integers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;I have 2 apples and 10 bananas.&quot;<\/p>\n<p>print(extract_integers(s))  # \u8f93\u51fa: [2, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u4f7f\u7528<code>char.isdigit()<\/code>\u65b9\u6cd5\u68c0\u67e5\u5b57\u7b26\u662f\u5426\u662f\u6570\u5b57\u3002\u5982\u679c\u662f\u6570\u5b57\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230<code>current_number<\/code>\u4e2d\u3002\u5982\u679c\u4e0d\u662f\u6570\u5b57\u4e14<code>current_number<\/code>\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06<code>current_number<\/code>\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u6dfb\u52a0\u5230\u7ed3\u679c\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7c7b\u4f3c\u4e8e\u4e0a\u9762\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u4f46\u66f4\u4fa7\u91cd\u4e8e\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u548c\u903b\u8f91\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_integers(s):<\/p>\n<p>    integers = []<\/p>\n<p>    number = 0<\/p>\n<p>    number_found = False<\/p>\n<p>    for char in s:<\/p>\n<p>        if char.isdigit():<\/p>\n<p>            number = number * 10 + int(char)<\/p>\n<p>            number_found = True<\/p>\n<p>        elif number_found:<\/p>\n<p>            integers.append(number)<\/p>\n<p>            number = 0<\/p>\n<p>            number_found = False<\/p>\n<p>    if number_found:<\/p>\n<p>        integers.append(number)<\/p>\n<p>    return integers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;I have 2 apples and 10 bananas.&quot;<\/p>\n<p>print(extract_integers(s))  # \u8f93\u51fa: [2, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u903b\u8f91\u5224\u65ad\u548c\u6570\u5b66\u8fd0\u7b97\u6765\u6784\u5efa\u6574\u6570\u3002\u5f53\u9047\u5230\u6570\u5b57\u5b57\u7b26\u65f6\uff0c\u5c06\u5176\u52a0\u5165\u5f53\u524d\u6570\u5b57\u3002\u5f53\u9047\u5230\u975e\u6570\u5b57\u5b57\u7b26\u65f6\uff0c\u5982\u679c\u4e4b\u524d\u5df2\u7ecf\u627e\u5230\u6570\u5b57\uff0c\u5219\u5c06\u5176\u52a0\u5165\u7ed3\u679c\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u6bd4\u8f83\u4e0e\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u65b9\u6cd5\u662f\u6700\u7075\u6d3b\u548c\u5f3a\u5927\u7684\uff0c\u9002\u7528\u4e8e\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\u3002<strong>\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong>\u5219\u66f4\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u5904\u7406\u3002<strong>\u904d\u5386\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong>\u867d\u7136\u7a0d\u5fae\u590d\u6742\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u5904\u7406\u7684\u662f\u590d\u6742\u7684\u5b57\u7b26\u4e32\u6a21\u5f0f\uff0c\u63a8\u8350\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5982\u679c\u662f\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u5904\u7406\uff0c\u5b57\u7b26\u4e32\u65b9\u6cd5\u548c\u904d\u5386\u5b57\u7b26\u4e32\u65b9\u6cd5\u90fd\u53ef\u4ee5\u8003\u8651\u3002<\/p>\n<\/p>\n<p><h3>\u4ee3\u7801\u4f18\u5316\u4e0e\u63d0\u793a<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5904\u7406\u8d1f\u6570\u548c\u5c0f\u6570<\/strong>\uff1a\u4e0a\u8ff0\u65b9\u6cd5\u4ec5\u9002\u7528\u4e8e\u63d0\u53d6\u6b63\u6574\u6570\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u8d1f\u6570\u6216\u5c0f\u6570\uff0c\u53ef\u4ee5\u8c03\u6574\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u6216\u5728\u904d\u5386\u5b57\u7b26\u4e32\u65f6\u589e\u52a0\u903b\u8f91\u5224\u65ad\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u5316<\/strong>\uff1a\u5bf9\u4e8e\u957f\u5b57\u7b26\u4e32\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5\u901a\u5e38\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u5176\u5e95\u5c42\u5b9e\u73b0\u7ecf\u8fc7\u9ad8\u5ea6\u4f18\u5316\u3002\u904d\u5386\u5b57\u7b26\u4e32\u65b9\u6cd5\u867d\u7136\u76f4\u89c2\uff0c\u4f46\u5728\u5904\u7406\u975e\u5e38\u957f\u7684\u5b57\u7b26\u4e32\u65f6\u53ef\u80fd\u4f1a\u7a0d\u6162\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u8003\u8651\u8f93\u5165\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u975e\u6807\u51c6\u5b57\u7b26\u6216\u7a7a\u5b57\u7b26\u4e32\u3002\u6dfb\u52a0\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u4f18\u5316\u7b56\u7565\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6574\u6570\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u4f18\u5316\uff0c\u80fd\u591f\u663e\u8457\u63d0\u5347\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u591a\u4e2a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u591a\u4e2a\u6574\u6570\u3002\u901a\u8fc7<code>re<\/code>\u6a21\u5757\u7684<code>findall()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u627e\u5230\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6570\u5b57\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\nstring = &quot;\u67093\u4e2a\u82f9\u679c\u548c5\u4e2a\u6a59\u5b50&quot;\nnumbers = re.findall(r&#39;\\d+&#39;, string)\nintegers = [int(num) for num in numbers]\nprint(integers)  # \u8f93\u51fa: [3, 5]\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u8d1f\u6570\uff0c\u5982\u4f55\u63d0\u53d6\uff1f<\/strong><br \/>\u63d0\u53d6\u8d1f\u6570\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9e\u73b0\u3002\u4f7f\u7528<code>-?\\d+<\/code>\u53ef\u4ee5\u5339\u914d\u8d1f\u6570\u548c\u6b63\u6570\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\nstring = &quot;\u6e29\u5ea6\u53d8\u5316\u8303\u56f4\u662f-10\u523020\u5ea6&quot;\nnumbers = re.findall(r&#39;-?\\d+&#39;, string)\nintegers = [int(num) for num in numbers]\nprint(integers)  # \u8f93\u51fa: [-10, 20]\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u6574\u6570\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6ca1\u6709\u6574\u6570\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u5728\u63d0\u53d6\u540e\u68c0\u67e5\u7ed3\u679c\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u6570\u5b57\uff0c\u53ef\u4ee5\u7ed9\u51fa\u76f8\u5e94\u7684\u63d0\u793a\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import re\n\nstring = &quot;\u6ca1\u6709\u6570\u5b57\u7684\u5b57\u7b26\u4e32&quot;\nnumbers = re.findall(r&#39;\\d+&#39;, string)\n\nif numbers:\n    integers = [int(num) for num in numbers]\nelse:\n    integers = []\n    print(&quot;\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u6574\u6570\u3002&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6574\u6570\uff0c\u5e76\u5904\u7406\u5404\u79cd\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6574\u6570\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u3001\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1053373,"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\/1053363"}],"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=1053363"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053363\/revisions"}],"predecessor-version":[{"id":1053377,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053363\/revisions\/1053377"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1053373"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1053363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1053363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1053363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}