{"id":1127545,"date":"2025-01-08T20:11:16","date_gmt":"2025-01-08T12:11:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1127545.html"},"modified":"2025-01-08T20:11:19","modified_gmt":"2025-01-08T12:11:19","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e5%8f%a5%e5%ad%90%e9%87%8c%e6%8a%8a%e5%ad%97%e7%ac%a6%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%95%b4%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1127545.html","title":{"rendered":"python\u5982\u4f55\u5728\u53e5\u5b50\u91cc\u628a\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094526\/2b77bf10-7b9f-4b19-94d2-8e95c2290625.webp\" alt=\"python\u5982\u4f55\u5728\u53e5\u5b50\u91cc\u628a\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u3001\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u5728\u6570\u636e\u5904\u7406\u3001\u6587\u672c\u5206\u6790\u548c\u5176\u4ed6\u7f16\u7a0b\u4efb\u52a1\u4e2d\u975e\u5e38\u5e38\u89c1\u3002\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u662f\u4ece\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5e76\u8f6c\u6362\u8fd9\u4e9b\u6570\u5b57\u4e3a\u6574\u6570\u3002Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u5982 <code>int()<\/code>\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u8981\u5728\u53e5\u5b50\u4e2d\u8f6c\u6362\u7279\u5b9a\u5b57\u7b26\u4e3a\u6574\u6570\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u548c\u5217\u8868\u89e3\u6790\u7b49\u6280\u672f\u3002<\/p>\n<\/p>\n<p><h4>1. \u63d0\u53d6\u548c\u8f6c\u6362\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u548c\u5176\u4ed6\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u8fd9\u4e9b\u6570\u5b57\uff0c\u5e76\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def extract_and_convert_to_int(sentence):<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6240\u6709\u7684\u6570\u5b57<\/p>\n<p>    numbers = re.findall(r&#39;\\d+&#39;, sentence)<\/p>\n<p>    # \u5c06\u63d0\u53d6\u5230\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570<\/p>\n<p>    integers = [int(num) for num in numbers]<\/p>\n<p>    return integers<\/p>\n<p>sentence = &quot;There are 3 apples, 4 bananas, and 5 oranges.&quot;<\/p>\n<p>result = extract_and_convert_to_int(sentence)<\/p>\n<p>print(result)  # \u8f93\u51fa: [3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f <code>\\d+<\/code> \u7528\u4e8e\u5339\u914d\u6240\u6709\u7684\u6570\u5b57\uff0c\u5e76\u4f7f\u7528 <code>int()<\/code> \u51fd\u6570\u5c06\u8fd9\u4e9b\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b57\u7b26\u4e32\u53ef\u80fd\u5305\u542b\u65e0\u6cd5\u76f4\u63a5\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u5b57\u7b26\u3002\u4e3a\u4e86\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>try-except<\/code> \u5757\u6765\u5904\u7406\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_convert_to_int(character):<\/p>\n<p>    try:<\/p>\n<p>        return int(character)<\/p>\n<p>    except ValueError:<\/p>\n<p>        return None<\/p>\n<p>sentence = &quot;The temperature is 30 degrees at 4 PM.&quot;<\/p>\n<p>characters = sentence.split()<\/p>\n<p>converted_integers = [safe_convert_to_int(char) for char in characters if safe_convert_to_int(char) is not None]<\/p>\n<p>print(converted_integers)  # \u8f93\u51fa: [30, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>safe_convert_to_int<\/code> \u51fd\u6570\u6765\u5b89\u5168\u5730\u8fdb\u884c\u8f6c\u6362\uff0c\u5e76\u5ffd\u7565\u65e0\u6cd5\u8f6c\u6362\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528\u5217\u8868\u89e3\u6790\u8fdb\u884c\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u6027\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u89e3\u6790\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u5b9e\u73b0\u5b57\u7b26\u5230\u6574\u6570\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u5217\u8868\u89e3\u6790<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">sentence = &quot;There are 3 dogs and 2 cats.&quot;<\/p>\n<p>integers = [int(char) for char in sentence.split() if char.isdigit()]<\/p>\n<p>print(integers)  # \u8f93\u51fa: [3, 2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>isdigit()<\/code> \u65b9\u6cd5\u6765\u68c0\u67e5\u5b57\u7b26\u662f\u5426\u4e3a\u6570\u5b57\uff0c\u5e76\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>2. \u590d\u6742\u7684\u5217\u8868\u89e3\u6790<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u590d\u6742\u7684\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u66f4\u590d\u6742\u7684\u5217\u8868\u89e3\u6790\u6765\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sentence = &quot;Item1 costs 10 dollars, Item2 costs 20 dollars.&quot;<\/p>\n<p>integers = [int(re.sub(r&#39;\\D&#39;, &#39;&#39;, char)) for char in sentence.split() if any(c.isdigit() for c in char)]<\/p>\n<p>print(integers)  # \u8f93\u51fa: [1, 10, 2, 20]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>re.sub<\/code> \u51fd\u6570\u6765\u53bb\u9664\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u5e76\u7ed3\u5408\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u5904\u7406\u7279\u5b9a\u9700\u6c42<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u7279\u5b9a\u9700\u6c42\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5904\u7406\u5b57\u7b26\u5230\u6574\u6570\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u81ea\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_convert(sentence):<\/p>\n<p>    result = []<\/p>\n<p>    for word in sentence.split():<\/p>\n<p>        try:<\/p>\n<p>            num = int(word)<\/p>\n<p>            result.append(num)<\/p>\n<p>        except ValueError:<\/p>\n<p>            continue<\/p>\n<p>    return result<\/p>\n<p>sentence = &quot;Age: 25, Height: 170cm, Weight: 65kg.&quot;<\/p>\n<p>converted = custom_convert(sentence)<\/p>\n<p>print(converted)  # \u8f93\u51fa: [25, 170, 65]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a <code>custom_convert<\/code> \u51fd\u6570\u6765\u5904\u7406\u53e5\u5b50\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd\uff0c\u5e76\u5c1d\u8bd5\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u5904\u7406\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u5b57\u7b26\u4e32\u5177\u6709\u7279\u5b9a\u683c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u66f4\u5177\u4f53\u7684\u51fd\u6570\u6765\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def convert_special_format(sentence):<\/p>\n<p>    matches = re.findall(r&#39;\\d+&#39;, sentence)<\/p>\n<p>    return [int(match) for match in matches]<\/p>\n<p>sentence = &quot;Order #123: 2 pizzas, 3 sodas, and 1 cake.&quot;<\/p>\n<p>converted = convert_special_format(sentence)<\/p>\n<p>print(converted)  # \u8f93\u51fa: [123, 2, 3, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u5b57\uff0c\u5e76\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5904\u7406\u6280\u672f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u636e\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5904\u7406\u6280\u672f\uff0c\u5982\u6570\u636e\u6e05\u6d17\u3001\u683c\u5f0f\u5316\u7b49\uff0c\u6765\u5b9e\u73b0\u5b57\u7b26\u5230\u6574\u6570\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def clean_and_convert(sentence):<\/p>\n<p>    cleaned_sentence = sentence.replace(&#39;,&#39;, &#39;&#39;).replace(&#39;.&#39;, &#39;&#39;)<\/p>\n<p>    numbers = re.findall(r&#39;\\d+&#39;, cleaned_sentence)<\/p>\n<p>    return [int(num) for num in numbers]<\/p>\n<p>sentence = &quot;The total cost is 1,234 dollars and 56 cents.&quot;<\/p>\n<p>converted = clean_and_convert(sentence)<\/p>\n<p>print(converted)  # \u8f93\u51fa: [1234, 56]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u6e05\u6d17\u6570\u636e\uff0c\u53bb\u9664\u9017\u53f7\u548c\u53e5\u53f7\uff0c\u7136\u540e\u63d0\u53d6\u5e76\u8f6c\u6362\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>2. \u683c\u5f0f\u5316\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def format_and_convert(sentence):<\/p>\n<p>    formatted_sentence = sentence.lower().replace(&#39;dollars&#39;, &#39;&#39;).replace(&#39;cents&#39;, &#39;&#39;)<\/p>\n<p>    numbers = re.findall(r&#39;\\d+&#39;, formatted_sentence)<\/p>\n<p>    return [int(num) for num in numbers]<\/p>\n<p>sentence = &quot;The total cost is 1,234 dollars and 56 cents.&quot;<\/p>\n<p>converted = format_and_convert(sentence)<\/p>\n<p>print(converted)  # \u8f93\u51fa: [1234, 56]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u683c\u5f0f\u5316\u6570\u636e\uff0c\u53bb\u9664\u7279\u5b9a\u7684\u5355\u8bcd\uff0c\u7136\u540e\u63d0\u53d6\u5e76\u8f6c\u6362\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f\u548c\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u5230\u6574\u6570\u7684\u8f6c\u6362\u5728\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u548c\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u6587\u672c\u683c\u5f0f\u5b58\u50a8\u3002\u6211\u4eec\u9700\u8981\u5c06\u8fd9\u4e9b\u6587\u672c\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u503c\u6570\u636e\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;description&#39;: [&#39;3 apples&#39;, &#39;4 bananas&#39;, &#39;5 oranges&#39;],<\/p>\n<p>    &#39;price&#39;: [&#39;1.50&#39;, &#39;2.00&#39;, &#39;2.50&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df[&#39;quantity&#39;] = df[&#39;description&#39;].apply(lambda x: int(re.search(r&#39;\\d+&#39;, x).group()))<\/p>\n<p>df[&#39;price&#39;] = df[&#39;price&#39;].astype(float)<\/p>\n<p>df[&#39;total&#39;] = df[&#39;quantity&#39;] * df[&#39;price&#39;]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 Pandas \u5e93\u6765\u5904\u7406\u6570\u636e\uff0c\u5e76\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u91cf\u548c\u4ef7\u683c\u8f6c\u6362\u4e3a\u6570\u503c\u3002<\/p>\n<\/p>\n<p><h4>2. \u6587\u672c\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u4e2d\uff0c\u6587\u672c\u6570\u636e\u7684\u5904\u7406\u548c\u8f6c\u6362\u662f\u5e38\u89c1\u4efb\u52a1\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6587\u672c\u4e2d\u7684\u6570\u5b57\u63d0\u53d6\u5e76\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4f5c\u4e3a\u7279\u5f81\u8fdb\u884c\u8fdb\u4e00\u6b65\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.feature_extraction.text import CountVectorizer<\/p>\n<p>corpus = [&quot;I have 2 cats&quot;, &quot;She has 3 dogs&quot;, &quot;We have 4 birds&quot;]<\/p>\n<p>vectorizer = CountVectorizer()<\/p>\n<p>X = vectorizer.fit_transform(corpus)<\/p>\n<p>print(vectorizer.get_feature_names_out())<\/p>\n<p>print(X.toarray())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 CountVectorizer \u5c06\u6587\u672c\u4e2d\u7684\u5355\u8bcd\u8f6c\u6362\u4e3a\u7279\u5f81\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>3. \u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406\u9636\u6bb5\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5c06\u5305\u542b\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def preprocess_data(data):<\/p>\n<p>    cleaned_data = []<\/p>\n<p>    for item in data:<\/p>\n<p>        numbers = re.findall(r&#39;\\d+&#39;, item)<\/p>\n<p>        cleaned_data.append([int(num) for num in numbers])<\/p>\n<p>    return cleaned_data<\/p>\n<p>data = [&quot;Item1: 100 units&quot;, &quot;Item2: 200 units&quot;, &quot;Item3: 300 units&quot;]<\/p>\n<p>preprocessed = preprocess_data(data)<\/p>\n<p>print(preprocessed)  # \u8f93\u51fa: [[100], [200], [300]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a <code>preprocess_data<\/code> \u51fd\u6570\u6765\u6e05\u6d17\u548c\u9884\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u6211\u4eec\u63a2\u8ba8\u4e86\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u5217\u8868\u89e3\u6790\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u4ee5\u53ca\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5904\u7406\u6280\u672f\u7684\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u6570\u636e\u5206\u6790\u3001\u6587\u672c\u5904\u7406\u3001\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406\u7b49\u9886\u57df\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u6838\u5fc3\u89c2\u70b9\u5305\u62ec\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u8f6c\u6362<\/strong>\uff1a\u4f8b\u5982 <code>int()<\/code> \u51fd\u6570\u3002<\/li>\n<li><strong>\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/strong>\uff1a\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/li>\n<li><strong>\u5229\u7528\u5217\u8868\u89e3\u6790\u8fdb\u884c\u8f6c\u6362<\/strong>\uff1a\u7b80\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u5904\u7406\u7279\u5b9a\u9700\u6c42<\/strong>\uff1a\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u5b9a\u5236\u3002<\/li>\n<li><strong>\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u5904\u7406\u6280\u672f<\/strong>\uff1a\u5982\u6570\u636e\u6e05\u6d17\u548c\u683c\u5f0f\u5316\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u548c\u8f6c\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u4e3a\u6574\u6570\uff0c\u6ee1\u8db3\u5404\u79cd\u5b9e\u9645\u5e94\u7528\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>int()<\/code>\u51fd\u6570\u6765\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6570\u5b57\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u6570\u5b57\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32<code>&quot;123&quot;<\/code>\uff0c\u4f7f\u7528<code>int(&quot;123&quot;)<\/code>\u5c06\u8fd4\u56de\u6574\u6570<code>123<\/code>\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u8f6c\u6362\u65f6\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u4f7f\u7528<code>str.isdigit()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u786e\u8ba4\u5b57\u7b26\u4e32\u662f\u6709\u6548\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7a7a\u683c\u6216\u5176\u4ed6\u5b57\u7b26\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>str.strip()<\/code>\u65b9\u6cd5\u6765\u53bb\u9664\u5b57\u7b26\u4e32\u9996\u5c3e\u7684\u7a7a\u683c\uff0c\u6216\u8005\u4f7f\u7528<code>str.replace()<\/code>\u65b9\u6cd5\u53bb\u6389\u4e0d\u9700\u8981\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32<code>&quot;  456  &quot;<\/code>\u53ef\u4ee5\u901a\u8fc7<code>int(&quot;  456  &quot;.strip())<\/code>\u8f6c\u6362\u4e3a\u6574\u6570<code>456<\/code>\u3002\u6ce8\u610f\uff0c\u786e\u4fdd\u5728\u8f6c\u6362\u4e4b\u524d\u53bb\u6389\u6240\u6709\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u4ee5\u907f\u514d\u8f6c\u6362\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u5305\u542b\u591a\u4e2a\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u5982\u4f55\u63d0\u53d6\u5e76\u8f6c\u6362\u6bcf\u4e2a\u6570\u5b57\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5757<code>re<\/code>\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002\u901a\u8fc7<code>re.findall(r&#39;\\d+&#39;, your_string)<\/code>\u53ef\u4ee5\u83b7\u53d6\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684\u6570\u5b57\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u8fd9\u4e9b\u6570\u5b57\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5b57\u7b26\u4e32<code>&quot;abc 123 def 456&quot;<\/code>\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>numbers = [int(num) for num in re.findall(r&#39;\\d+&#39;, &quot;abc 123 def 456&quot;)]<\/code>\uff0c\u7ed3\u679c\u5c06\u662f\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u5217\u8868<code>[123, 456]<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u8f6c\u6362\u4e3a\u6574\u6570\u3001\u4f7f\u7528int()\u51fd\u6570\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5 \u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57 [&hellip;]","protected":false},"author":3,"featured_media":1127553,"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\/1127545"}],"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=1127545"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127545\/revisions"}],"predecessor-version":[{"id":1127556,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1127545\/revisions\/1127556"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1127553"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1127545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1127545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1127545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}