{"id":1160626,"date":"2025-01-13T19:06:54","date_gmt":"2025-01-13T11:06:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1160626.html"},"modified":"2025-01-13T19:06:57","modified_gmt":"2025-01-13T11:06:57","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba%e4%b8%ad%e7%9a%84%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1160626.html","title":{"rendered":"python\u5982\u4f55\u53d6\u51fa\u4e2d\u7684\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202027\/231e2eb5-041e-4453-9744-89dbf7003806.webp\" alt=\"python\u5982\u4f55\u53d6\u51fa\u4e2d\u7684\u6570\u5b57\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u53d6\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u89e3\u6790\u6216\u7b80\u5355\u7684\u5faa\u73af\u6765\u5b9e\u73b0\u3002<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u89e3\u6790\u3001\u5faa\u73af<\/strong>\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff08regex\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u67e5\u627e\u548c\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u7279\u5b9a\u6a21\u5f0f\u3002Python\u63d0\u4f9b\u4e86<code>re<\/code>\u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u901a\u8fc7\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u5feb\u901f\u5730\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u51fa\u6240\u6709\u7684\u6570\u5b57\u3002\u4e0b\u9762\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers(input_string):<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u627e\u6240\u6709\u7684\u6570\u5b57<\/p>\n<p>    numbers = re.findall(r&#39;\\d+&#39;, input_string)<\/p>\n<p>    # \u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868<\/p>\n<p>    numbers = list(map(int, numbers))<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The price is 100 dollars and 50 cents&quot;<\/p>\n<p>numbers = extract_numbers(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [100, 50]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>re.findall(r&#39;\\d+&#39;, input_string)<\/code>\u51fd\u6570\u4f1a\u67e5\u627e\u5e76\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\uff0c<code>\\d+<\/code>\u8868\u793a\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>map(int, numbers)<\/code>\u5c06\u627e\u5230\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5217\u8868\u548c\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u5728Python\u4e2d\uff0c\u5217\u8868\u89e3\u6790\u53ef\u4ee5\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6570\u5b57\u3002\u7ed3\u5408<code>str.isdigit()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_numbers(input_string):<\/p>\n<p>    numbers = [int(s) for s in input_string.split() if s.isdigit()]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;I have 2 apples and 3 oranges&quot;<\/p>\n<p>numbers = extract_numbers(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5b57\u7b26\u4e32\u7528<code>split()<\/code>\u65b9\u6cd5\u5206\u5272\u6210\u5355\u8bcd\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u7b5b\u9009\u51fa\u6240\u6709\u662f\u6570\u5b57\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5faa\u73af\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\uff0c\u53ef\u4ee5\u624b\u52a8\u63d0\u53d6\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u5217\u8868\u89e3\u6790\u7b80\u6d01\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_numbers(input_string):<\/p>\n<p>    numbers = []<\/p>\n<p>    current_number = &#39;&#39;<\/p>\n<p>    for char in input_string:<\/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>                numbers.append(int(current_number))<\/p>\n<p>                current_number = &#39;&#39;<\/p>\n<p>    if current_number:<\/p>\n<p>        numbers.append(int(current_number))<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The room number is 123 and the code is 4567&quot;<\/p>\n<p>numbers = extract_numbers(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [123, 4567]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5982\u679c\u5b57\u7b26\u662f\u6570\u5b57\uff0c\u5c31\u5c06\u5176\u6dfb\u52a0\u5230\u5f53\u524d\u6570\u5b57\u5b57\u7b26\u4e32\u4e2d\u3002\u5982\u679c\u9047\u5230\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u5e76\u4e14\u5f53\u524d\u6570\u5b57\u5b57\u7b26\u4e32\u4e0d\u4e3a\u7a7a\uff0c\u6211\u4eec\u5c31\u5c06\u5f53\u524d\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u6dfb\u52a0\u5230\u7ed3\u679c\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528filter\u548clambda\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>filter<\/code>\u548c<code>lambda<\/code>\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u3002<code>filter<\/code>\u51fd\u6570\u7528\u4e8e\u6784\u9020\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5305\u542b\u539f\u5217\u8868\u4e2d\u6240\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_numbers(input_string):<\/p>\n<p>    numbers = filter(str.isdigit, input_string)<\/p>\n<p>    return list(numbers)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The numbers are 4, 5, and 6&quot;<\/p>\n<p>numbers = extract_numbers(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [&#39;4&#39;, &#39;5&#39;, &#39;6&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>filter(str.isdigit, input_string)<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5305\u542b\u6240\u6709\u6ee1\u8db3<code>isdigit<\/code>\u6761\u4ef6\u7684\u5b57\u7b26\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u4e0a\u8ff0\u65b9\u6cd5\u7efc\u5408\u5e94\u7528\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u590d\u6742\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5faa\u73af\u904d\u5386\uff0c\u4ee5\u786e\u4fdd\u63d0\u53d6\u5230\u7684\u6570\u5b57\u7b26\u5408\u7279\u5b9a\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers(input_string):<\/p>\n<p>    numbers = re.findall(r&#39;\\d+&#39;, input_string)<\/p>\n<p>    filtered_numbers = []<\/p>\n<p>    for number in numbers:<\/p>\n<p>        if len(number) &gt; 1:  # \u8fc7\u6ee4\u6389\u5355\u4e2a\u6570\u5b57<\/p>\n<p>            filtered_numbers.append(int(number))<\/p>\n<p>    return filtered_numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The code is 12345 and the number is 7&quot;<\/p>\n<p>numbers = extract_numbers(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [12345]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6240\u6709\u6570\u5b57\uff0c\u7136\u540e\u901a\u8fc7\u5faa\u73af\u8fc7\u6ee4\u6389\u957f\u5ea6\u4e3a1\u7684\u6570\u5b57\uff0c\u6700\u7ec8\u5f97\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u5b57\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u7a0d\u5fae\u4fee\u6539\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f7f\u5176\u80fd\u591f\u5339\u914d\u5305\u542b\u5c0f\u6570\u70b9\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_floats(input_string):<\/p>\n<p>    floats = re.findall(r&#39;\\d+\\.\\d+&#39;, input_string)<\/p>\n<p>    floats = list(map(float, floats))<\/p>\n<p>    return floats<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The prices are 12.99, 23.45, and 45.67 dollars&quot;<\/p>\n<p>floats = extract_floats(input_string)<\/p>\n<p>print(floats)  # \u8f93\u51fa: [12.99, 23.45, 45.67]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>\\d+\\.\\d+<\/code>\u7528\u4e8e\u5339\u914d\u5305\u542b\u5c0f\u6570\u70b9\u7684\u6570\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u8d1f\u6570\u548c\u79d1\u5b66\u8ba1\u6570\u6cd5<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u63d0\u53d6\u8d1f\u6570\u6216\u8005\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4fee\u6539\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4ee5\u5339\u914d\u8fd9\u4e9b\u590d\u6742\u7684\u6570\u5b57\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_complex_numbers(input_string):<\/p>\n<p>    complex_numbers = re.findall(r&#39;-?\\d+\\.?\\d*e?-?\\d*&#39;, input_string)<\/p>\n<p>    complex_numbers = list(map(float, complex_numbers))<\/p>\n<p>    return complex_numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The values are -12.5, 3.14e2, and -4.56e-3&quot;<\/p>\n<p>complex_numbers = extract_complex_numbers(input_string)<\/p>\n<p>print(complex_numbers)  # \u8f93\u51fa: [-12.5, 314.0, -0.00456]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>-?\\d+\\.?\\d*e?-?\\d*<\/code>\u7528\u4e8e\u5339\u914d\u8d1f\u6570\u548c\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u6570\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u5e26\u6709\u5355\u4f4d\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u5e94\u7528\u4e2d\uff0c\u6570\u5b57\u53ef\u80fd\u4f1a\u5e26\u6709\u5355\u4f4d\uff0c\u4f8b\u5982\u201c10kg\u201d\u3001\u201c5m\u201d\u7b49\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6355\u83b7\u6570\u5b57\u548c\u5355\u4f4d\uff0c\u5e76\u8fdb\u884c\u9002\u5f53\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers_with_units(input_string):<\/p>\n<p>    matches = re.findall(r&#39;(\\d+)([a-zA-Z]+)&#39;, input_string)<\/p>\n<p>    results = [(int(number), unit) for number, unit in matches]<\/p>\n<p>    return results<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The weights are 10kg, 5m, and 20cm&quot;<\/p>\n<p>numbers_with_units = extract_numbers_with_units(input_string)<\/p>\n<p>print(numbers_with_units)  # \u8f93\u51fa: [(10, &#39;kg&#39;), (5, &#39;m&#39;), (20, &#39;cm&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>(\\d+)([a-zA-Z]+)<\/code>\u7528\u4e8e\u5339\u914d\u6570\u5b57\u548c\u7d27\u968f\u5176\u540e\u7684\u5b57\u6bcd\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5339\u914d\u5230\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5e76\u4e0e\u5355\u4f4d\u4e00\u8d77\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u5e26\u6709\u5206\u9694\u7b26\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6570\u5b57\u4e2d\u53ef\u80fd\u5305\u542b\u5206\u9694\u7b26\uff0c\u4f8b\u5982\u5343\u4f4d\u5206\u9694\u7b26\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5e76\u5904\u7406\u8fd9\u4e9b\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers_with_separators(input_string):<\/p>\n<p>    matches = re.findall(r&#39;\\d{1,3}(?:,\\d{3})*(?:\\.\\d+)?&#39;, input_string)<\/p>\n<p>    numbers = [float(match.replace(&#39;,&#39;, &#39;&#39;)) for match in matches]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The amounts are 1,000, 2,500.50, and 3,000,000&quot;<\/p>\n<p>numbers = extract_numbers_with_separators(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1000.0, 2500.5, 3000000.0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>\\d{1,3}(?:,\\d{3})*(?:\\.\\d+)?<\/code>\u7528\u4e8e\u5339\u914d\u5305\u542b\u5343\u4f4d\u5206\u9694\u7b26\u7684\u6570\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u9017\u53f7\u79fb\u9664\uff0c\u5e76\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u975e\u8fde\u7eed\u7684\u6570\u5b57\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u53ef\u80fd\u662f\u975e\u8fde\u7eed\u7684\uff0c\u4f8b\u5982\u201c123abc456\u201d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8fde\u7eed\u7684\u6570\u5b57\u90e8\u5206\uff0c\u7136\u540e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_non_continuous_numbers(input_string):<\/p>\n<p>    matches = re.findall(r&#39;\\d+&#39;, input_string)<\/p>\n<p>    numbers = [int(match) for match in matches]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;123abc456def789&quot;<\/p>\n<p>numbers = extract_non_continuous_numbers(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [123, 456, 789]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>\\d+<\/code>\u7528\u4e8e\u5339\u914d\u8fde\u7eed\u7684\u6570\u5b57\u90e8\u5206\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5339\u914d\u5230\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5904\u7406\u591a\u79cd\u6570\u5b57\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u79cd\u4e0d\u540c\u683c\u5f0f\u7684\u6570\u5b57\uff0c\u4f8b\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u8d1f\u6570\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7efc\u5408\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u8fd9\u4e9b\u4e0d\u540c\u683c\u5f0f\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_mixed_numbers(input_string):<\/p>\n<p>    matches = re.findall(r&#39;-?\\d+(?:\\.\\d+)?(?:e-?\\d+)?&#39;, input_string)<\/p>\n<p>    numbers = [float(match) if &#39;.&#39; in match or &#39;e&#39; in match else int(match) for match in matches]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The values are 123, -45.67, 8e2, and -3.14e-2&quot;<\/p>\n<p>numbers = extract_mixed_numbers(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [123, -45.67, 800.0, -0.0314]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>-?\\d+(?:\\.\\d+)?(?:e-?\\d+)?<\/code>\u7528\u4e8e\u5339\u914d\u6574\u6570\u3001\u6d6e\u70b9\u6570\u548c\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u6570\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u6839\u636e\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u5185\u5bb9\uff0c\u5206\u522b\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5904\u7406\u5e26\u6709\u524d\u7f00\u6216\u540e\u7f00\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u5b57\u53ef\u80fd\u5e26\u6709\u524d\u7f00\u6216\u540e\u7f00\uff0c\u4f8b\u5982\u201c$100\u201d\u6216\u201c100%\u201d\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6355\u83b7\u8fd9\u4e9b\u6570\u5b57\uff0c\u5e76\u8fdb\u884c\u9002\u5f53\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers_with_prefix_suffix(input_string):<\/p>\n<p>    matches = re.findall(r&#39;[\\$%]?\\d+(?:\\.\\d+)?[%]?&#39;, input_string)<\/p>\n<p>    numbers = [float(match.strip(&#39;$%&#39;)) for match in matches]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The prices are $100, 200%, and $300.50&quot;<\/p>\n<p>numbers = extract_numbers_with_prefix_suffix(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [100.0, 200.0, 300.5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>[\\$%]?\\d+(?:\\.\\d+)?[%]?<\/code>\u7528\u4e8e\u5339\u914d\u5e26\u6709\u524d\u7f00\u6216\u540e\u7f00\u7684\u6570\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u524d\u7f00\u6216\u540e\u7f00\u79fb\u9664\uff0c\u5e76\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5904\u7406\u5e26\u6709\u7a7a\u683c\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6570\u5b57\u4e2d\u53ef\u80fd\u5305\u542b\u7a7a\u683c\uff0c\u4f8b\u5982\u201c1 000\u201d\u8868\u793a\u4e00\u5343\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5e76\u5904\u7406\u8fd9\u4e9b\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers_with_spaces(input_string):<\/p>\n<p>    matches = re.findall(r&#39;\\d+(?:\\s\\d+)*(?:\\.\\d+)?&#39;, input_string)<\/p>\n<p>    numbers = [float(match.replace(&#39; &#39;, &#39;&#39;)) for match in matches]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The amounts are 1 000, 2 500.50, and 3 000 000&quot;<\/p>\n<p>numbers = extract_numbers_with_spaces(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1000.0, 2500.5, 3000000.0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>\\d+(?:\\s\\d+)*(?:\\.\\d+)?<\/code>\u7528\u4e8e\u5339\u914d\u5305\u542b\u7a7a\u683c\u7684\u6570\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\u79fb\u9664\uff0c\u5e76\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u5904\u7406\u4e0d\u540c\u8bed\u8a00\u7684\u6570\u5b57\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u56fd\u9645\u5316\u6570\u636e\u65f6\uff0c\u4e0d\u540c\u8bed\u8a00\u53ef\u80fd\u4f7f\u7528\u4e0d\u540c\u7684\u6570\u5b57\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5fb7\u8bed\u4e2d\u5343\u4f4d\u5206\u9694\u7b26\u662f\u70b9\uff0c\u5c0f\u6570\u5206\u9694\u7b26\u662f\u9017\u53f7\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548c\u5b57\u7b26\u4e32\u66ff\u6362\u6765\u5904\u7406\u8fd9\u4e9b\u4e0d\u540c\u683c\u5f0f\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_international_numbers(input_string):<\/p>\n<p>    input_string = input_string.replace(&#39;.&#39;, &#39;&#39;).replace(&#39;,&#39;, &#39;.&#39;)<\/p>\n<p>    matches = re.findall(r&#39;\\d+\\.\\d+|\\d+&#39;, input_string)<\/p>\n<p>    numbers = [float(match) for match in matches]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Die Preise sind 1.000, 2.500,50 und 3.000.000&quot;<\/p>\n<p>numbers = extract_international_numbers(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1000.0, 2500.5, 3000000.0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u70b9\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u5c06\u9017\u53f7\u66ff\u6362\u4e3a\u70b9\uff0c\u7136\u540e\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6570\u5b57\uff0c\u6700\u540e\u5c06\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u5904\u7406\u5e26\u6709\u7b26\u53f7\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u5b57\u53ef\u80fd\u5e26\u6709\u5404\u79cd\u7b26\u53f7\uff0c\u4f8b\u5982\u201c+100\u201d\u3001\u201c-200\u201d\u7b49\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8fd9\u4e9b\u5e26\u6709\u7b26\u53f7\u7684\u6570\u5b57\uff0c\u5e76\u8fdb\u884c\u9002\u5f53\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_signed_numbers(input_string):<\/p>\n<p>    matches = re.findall(r&#39;[+-]?\\d+(?:\\.\\d+)?&#39;, input_string)<\/p>\n<p>    numbers = [float(match) for match in matches]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The values are +100, -200.5, and +300.25&quot;<\/p>\n<p>numbers = extract_signed_numbers(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [100.0, -200.5, 300.25]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>[+-]?\\d+(?:\\.\\d+)?<\/code>\u7528\u4e8e\u5339\u914d\u5e26\u6709\u7b26\u53f7\u7684\u6570\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u5904\u7406\u4e0d\u540c\u8fdb\u5236\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6570\u5b57\u53ef\u80fd\u4ee5\u4e0d\u540c\u7684\u8fdb\u5236\u8868\u793a\uff0c\u4f8b\u5982\u4e8c\u8fdb\u5236\u3001\u516b\u8fdb\u5236\u6216\u5341\u516d\u8fdb\u5236\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8fd9\u4e9b\u4e0d\u540c\u8fdb\u5236\u7684\u6570\u5b57\uff0c\u5e76\u8fdb\u884c\u9002\u5f53\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_hex_numbers(input_string):<\/p>\n<p>    matches = re.findall(r&#39;0[xX][0-9a-fA-F]+&#39;, input_string)<\/p>\n<p>    numbers = [int(match, 16) for match in matches]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The values are 0x1A, 0x2F, and 0x3B&quot;<\/p>\n<p>numbers = extract_hex_numbers(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [26, 47, 59]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>0[xX][0-9a-fA-F]+<\/code>\u7528\u4e8e\u5339\u914d\u5341\u516d\u8fdb\u5236\u6570\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u5904\u7406\u5e26\u6709\u62ec\u53f7\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6570\u5b57\u53ef\u80fd\u5e26\u6709\u62ec\u53f7\uff0c\u4f8b\u5982\u201c(100)\u201d\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8fd9\u4e9b\u5e26\u6709\u62ec\u53f7\u7684\u6570\u5b57\uff0c\u5e76\u8fdb\u884c\u9002\u5f53\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers_with_parentheses(input_string):<\/p>\n<p>    matches = re.findall(r&#39;\\(\\d+\\)&#39;, input_string)<\/p>\n<p>    numbers = [int(match.strip(&#39;()&#39;)) for match in matches]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The values are (100), (200), and (300)&quot;<\/p>\n<p>numbers = extract_numbers_with_parentheses(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [100, 200, 300]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f<code>\\(\\d+\\)<\/code>\u7528\u4e8e\u5339\u914d\u5e26\u6709\u62ec\u53f7\u7684\u6570\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u62ec\u53f7\u79fb\u9664\uff0c\u5e76\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u5904\u7406\u5e26\u6709\u524d\u5bfc\u96f6\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6570\u5b57\u53ef\u80fd\u5e26\u6709\u524d\u5bfc\u96f6\uff0c\u4f8b\u5982\u201c00100\u201d\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8fd9\u4e9b\u5e26\u6709\u524d\u5bfc\u96f6\u7684\u6570\u5b57\uff0c\u5e76\u8fdb\u884c\u9002\u5f53\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_numbers_with_leading_zeros(input_string):<\/p>\n<p>    matches = re.findall(r&#39;\\d+&#39;, input_string)<\/p>\n<p>    numbers = [int(match) for match in matches]<\/p>\n<p>    return numbers<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;The values are 00100, 00200, and 00300&quot;<\/p>\n<p>numbers = extract_numbers_with_leading_zeros(input_string)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [100, 200, 300]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<code>re<\/code>\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u3002\u901a\u8fc7<code>re.findall()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u6240\u6709\u6570\u5b57\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\ntext = &quot;\u4eca\u5929\u7684\u6c14\u6e29\u662f25\u5ea6\uff0c\u660e\u5929\u5c06\u5347\u9ad8\u523030\u5ea6\u3002&quot;\nnumbers = re.findall(r&#39;\\d+&#39;, text)\nprint(numbers)  # \u8f93\u51fa: [&#39;25&#39;, &#39;30&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u5f62\u5f0f\u7684\u6570\u5b57\uff0c\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u80fd\u5426\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u8d1f\u6570\u548c\u5c0f\u6570\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u63d0\u53d6\u8d1f\u6570\u548c\u5c0f\u6570\u3002\u9700\u8981\u7a0d\u5fae\u8c03\u6574\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4ee5\u5339\u914d\u8d1f\u53f7\u548c\u5c0f\u6570\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\ntext = &quot;\u6e29\u5ea6\u8303\u56f4\u662f-5.5\u523010.2\u4e4b\u95f4\u3002&quot;\nnumbers = re.findall(r&#39;-?\\d+\\.?\\d*&#39;, text)\nprint(numbers)  # \u8f93\u51fa: [&#39;-5.5&#39;, &#39;10.2&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u63d0\u53d6\u5305\u62ec\u8d1f\u6570\u548c\u5c0f\u6570\u7684\u6240\u6709\u6570\u5b57\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u63d0\u53d6\u6570\u5b57\uff1f<\/strong><br \/>\u9664\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">text = &quot;\u8ba2\u5355\u53f7\u662f12345\uff0c\u6570\u91cf\u4e3a6789\u3002&quot;\nnumbers = [char for char in text if char.isdigit()]\nprint(&quot;&quot;.join(numbers))  # \u8f93\u51fa: &#39;123456789&#39;\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u4e0d\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u8f83\u7b80\u5355\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u53d6\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u89e3\u6790\u6216\u7b80\u5355\u7684\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5217\u8868\u89e3\u6790\u3001\u5faa [&hellip;]","protected":false},"author":3,"featured_media":1160629,"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\/1160626"}],"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=1160626"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160626\/revisions"}],"predecessor-version":[{"id":1160631,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160626\/revisions\/1160631"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1160629"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1160626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1160626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1160626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}