{"id":1113073,"date":"2025-01-08T17:44:22","date_gmt":"2025-01-08T09:44:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113073.html"},"modified":"2025-01-08T17:44:24","modified_gmt":"2025-01-08T09:44:24","slug":"python%e5%87%bd%e6%95%b0%e8%bf%94%e5%9b%9e%e4%b8%a4%e4%b8%aa%e5%88%97%e8%a1%a8%e5%a6%82%e4%bd%95%e5%8c%ba%e5%88%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113073.html","title":{"rendered":"python\u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u5217\u8868\u5982\u4f55\u533a\u5206"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074823\/45a548ad-dc71-404e-82a0-996df45d4292.webp\" alt=\"python\u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u5217\u8868\u5982\u4f55\u533a\u5206\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u65f6\u3002\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u8fd9\u4e9b\u503c\u53ef\u4ee5\u662f\u4e0d\u540c\u7c7b\u578b\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u76f8\u540c\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\u3002\u82e5\u51fd\u6570\u9700\u8981\u8fd4\u56de\u4e24\u4e2a\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5143\u7ec4\u6765\u7ec4\u5408\u8fd9\u4e9b\u5217\u8868\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u4e0d\u4ec5\u80fd\u591f\u8fd4\u56de\u591a\u4e2a\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u5728\u8c03\u7528\u51fd\u6570\u65f6\u8f7b\u677e\u5730\u5206\u89e3\u8fd9\u4e9b\u8fd4\u56de\u503c\u3002<strong>\u4f7f\u7528\u5143\u7ec4\u3001\u89e3\u5305\u8fd4\u56de\u503c<\/strong>\u662f\u533a\u5206\u548c\u5904\u7406\u591a\u4e2a\u8fd4\u56de\u5217\u8868\u7684\u6709\u6548\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5143\u7ec4\u8fd4\u56de\u591a\u4e2a\u5217\u8868<\/strong>\uff1aPython\u4e2d\u7684\u5143\u7ec4\u662f\u4e00\u79cd\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\u7c7b\u578b\uff0c\u9002\u5408\u7528\u6765\u7ec4\u5408\u591a\u4e2a\u8fd4\u56de\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5143\u7ec4\u8fd4\u56de\u4e24\u4e2a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_lists():<\/p>\n<p>    list1 = [1, 2, 3, 4, 5]<\/p>\n<p>    list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]<\/p>\n<p>    return list1, list2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c<code>list1<\/code> \u548c <code>list2<\/code> \u88ab\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\u8fd4\u56de\u3002\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u89e3\u5305\u64cd\u4f5c\u7b26 <code>,<\/code> \u6765\u5206\u89e3\u5143\u7ec4\u5e76\u83b7\u53d6\u4e24\u4e2a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1, list2 = generate_lists()<\/p>\n<p>print(&quot;List 1:&quot;, list1)<\/p>\n<p>print(&quot;List 2:&quot;, list2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u6e05\u6670\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u786e\u4fdd\u8fd4\u56de\u7684\u591a\u4e2a\u5217\u8868\u80fd\u591f\u88ab\u6b63\u786e\u5730\u533a\u5206\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5143\u7ec4\u8fd4\u56de\u591a\u4e2a\u5217\u8868<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5143\u7ec4\u8fd4\u56de\u591a\u4e2a\u5217\u8868\u7684\u6700\u5927\u4f18\u70b9\u662f\u7b80\u5355\u660e\u4e86\u3002\u901a\u8fc7\u5143\u7ec4\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u591a\u4e2a\u5217\u8868\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u5e76\u5728\u51fd\u6570\u8c03\u7528\u65f6\u8f7b\u677e\u5730\u89e3\u5305\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5143\u7ec4\u8fd4\u56de\u591a\u4e2a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_data(data):<\/p>\n<p>    even_numbers = []<\/p>\n<p>    odd_numbers = []<\/p>\n<p>    for num in data:<\/p>\n<p>        if num % 2 == 0:<\/p>\n<p>            even_numbers.append(num)<\/p>\n<p>        else:<\/p>\n<p>            odd_numbers.append(num)<\/p>\n<p>    return even_numbers, odd_numbers<\/p>\n<p>data = [10, 15, 20, 25, 30, 35, 40]<\/p>\n<p>evens, odds = process_data(data)<\/p>\n<p>print(&quot;Even numbers:&quot;, evens)<\/p>\n<p>print(&quot;Odd numbers:&quot;, odds)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>process_data<\/code> \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6570\u636e\u5217\u8868\uff0c\u5e76\u5c06\u5176\u4e2d\u7684\u5076\u6570\u548c\u5947\u6570\u5206\u5f00\u5b58\u50a8\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u5217\u8868\u4e2d\u3002\u7136\u540e\uff0c\u8fd9\u4e24\u4e2a\u5217\u8868\u88ab\u6253\u5305\u6210\u4e00\u4e2a\u5143\u7ec4\u8fd4\u56de\u3002\u5728\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u5143\u7ec4\u88ab\u89e3\u5305\u6210\u4e24\u4e2a\u72ec\u7acb\u7684\u5217\u8868 <code>evens<\/code> \u548c <code>odds<\/code>\uff0c\u5e76\u5206\u522b\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5b57\u5178\u8fd4\u56de\u591a\u4e2a\u5217\u8868<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5143\u7ec4\uff0c\u5b57\u5178\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u8fd4\u56de\u591a\u4e2a\u5217\u8868\u7684\u65b9\u6cd5\u3002\u5b57\u5178\u5141\u8bb8\u4f7f\u7528\u952e\u503c\u5bf9\u6765\u5b58\u50a8\u6570\u636e\uff0c\u8fd9\u4f7f\u5f97\u8fd4\u56de\u7684\u5217\u8868\u53ef\u4ee5\u901a\u8fc7\u952e\u540d\u8fdb\u884c\u8bbf\u95ee\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def analyze_data(data):<\/p>\n<p>    analysis = {<\/p>\n<p>        &#39;positives&#39;: [],<\/p>\n<p>        &#39;negatives&#39;: [],<\/p>\n<p>        &#39;zeros&#39;: []<\/p>\n<p>    }<\/p>\n<p>    for num in data:<\/p>\n<p>        if num &gt; 0:<\/p>\n<p>            analysis[&#39;positives&#39;].append(num)<\/p>\n<p>        elif num &lt; 0:<\/p>\n<p>            analysis[&#39;negatives&#39;].append(num)<\/p>\n<p>        else:<\/p>\n<p>            analysis[&#39;zeros&#39;].append(num)<\/p>\n<p>    return analysis<\/p>\n<p>data = [4, -1, 0, 7, -3, 0, 5, -8]<\/p>\n<p>result = analyze_data(data)<\/p>\n<p>print(&quot;Positives:&quot;, result[&#39;positives&#39;])<\/p>\n<p>print(&quot;Negatives:&quot;, result[&#39;negatives&#39;])<\/p>\n<p>print(&quot;Zeros:&quot;, result[&#39;zeros&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>analyze_data<\/code> \u51fd\u6570\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u548c\u8fd4\u56de\u591a\u4e2a\u5217\u8868\u3002\u5b57\u5178\u7684\u952e\u540d\uff08\u4f8b\u5982 &#39;positives&#39;, &#39;negatives&#39;, &#39;zeros&#39;\uff09\u6e05\u6670\u5730\u8868\u660e\u4e86\u6bcf\u4e2a\u5217\u8868\u7684\u7528\u9014\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u4f7f\u8fd4\u56de\u7684\u591a\u4e2a\u5217\u8868\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u8bbf\u95ee\uff0c\u8fd8\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u8fd4\u56de\u591a\u4e2a\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u573a\u666f\u4e0b\uff0c\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u6765\u8fd4\u56de\u591a\u4e2a\u5217\u8868\u53ef\u80fd\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u3002\u81ea\u5b9a\u4e49\u7c7b\u5141\u8bb8\u5f00\u53d1\u8005\u5b9a\u4e49\u66f4\u52a0\u4e30\u5bcc\u7684\u6570\u636e\u7ed3\u6784\u548c\u65b9\u6cd5\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u5f3a\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class DataAnalysis:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.evens = []<\/p>\n<p>        self.odds = []<\/p>\n<p>    def add_number(self, num):<\/p>\n<p>        if num % 2 == 0:<\/p>\n<p>            self.evens.append(num)<\/p>\n<p>        else:<\/p>\n<p>            self.odds.append(num)<\/p>\n<p>def analyze_numbers(data):<\/p>\n<p>    analysis = DataAnalysis()<\/p>\n<p>    for num in data:<\/p>\n<p>        analysis.add_number(num)<\/p>\n<p>    return analysis<\/p>\n<p>data = [2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<p>analysis = analyze_numbers(data)<\/p>\n<p>print(&quot;Even numbers:&quot;, analysis.evens)<\/p>\n<p>print(&quot;Odd numbers:&quot;, analysis.odds)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>DataAnalysis<\/code> \u7c7b\u88ab\u5b9a\u4e49\u4e3a\u5b58\u50a8\u548c\u5904\u7406\u5076\u6570\u548c\u5947\u6570\u5217\u8868\u3002<code>analyze_numbers<\/code> \u51fd\u6570\u521b\u5efa\u4e00\u4e2a <code>DataAnalysis<\/code> \u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5b83\u6765\u5904\u7406\u8f93\u5165\u6570\u636e\u3002\u6700\u7ec8\uff0c<code>DataAnalysis<\/code> \u5bf9\u8c61\u88ab\u8fd4\u56de\uff0c\u5e76\u901a\u8fc7\u5176\u5c5e\u6027\u8bbf\u95ee\u5076\u6570\u548c\u5947\u6570\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5f3a\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\uff0c\u9002\u7528\u4e8e\u9700\u8981\u590d\u6742\u6570\u636e\u5904\u7406\u548c\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u547d\u540d\u5143\u7ec4\u8fd4\u56de\u591a\u4e2a\u5217\u8868<\/p>\n<\/p>\n<p><p>\u547d\u540d\u5143\u7ec4\u662fPython\u6807\u51c6\u5e93 <code>collections<\/code> \u6a21\u5757\u4e2d\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4e3a\u5143\u7ec4\u7684\u6bcf\u4e2a\u4f4d\u7f6e\u8d4b\u4e88\u4e86\u4e00\u4e2a\u540d\u79f0\uff0c\u4ece\u800c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u6613\u8bfb\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u547d\u540d\u5143\u7ec4\u8fd4\u56de\u591a\u4e2a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import namedtuple<\/p>\n<p>def categorize_numbers(data):<\/p>\n<p>    CategorizedNumbers = namedtuple(&#39;CategorizedNumbers&#39;, [&#39;positives&#39;, &#39;negatives&#39;, &#39;zeros&#39;])<\/p>\n<p>    positives = []<\/p>\n<p>    negatives = []<\/p>\n<p>    zeros = []<\/p>\n<p>    for num in data:<\/p>\n<p>        if num &gt; 0:<\/p>\n<p>            positives.append(num)<\/p>\n<p>        elif num &lt; 0:<\/p>\n<p>            negatives.append(num)<\/p>\n<p>        else:<\/p>\n<p>            zeros.append(num)<\/p>\n<p>    return CategorizedNumbers(positives, negatives, zeros)<\/p>\n<p>data = [1, -2, 0, 4, -5, 0, 7, -8]<\/p>\n<p>categorized = categorize_numbers(data)<\/p>\n<p>print(&quot;Positives:&quot;, categorized.positives)<\/p>\n<p>print(&quot;Negatives:&quot;, categorized.negatives)<\/p>\n<p>print(&quot;Zeros:&quot;, categorized.zeros)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>CategorizedNumbers<\/code> \u662f\u4e00\u4e2a\u547d\u540d\u5143\u7ec4\uff0c\u5177\u6709\u4e09\u4e2a\u5b57\u6bb5\uff1apositives, negatives \u548c zeros\u3002<code>categorize_numbers<\/code> \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a <code>CategorizedNumbers<\/code> \u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u4e09\u4e2a\u5217\u8868\u3002\u901a\u8fc7\u4f7f\u7528\u547d\u540d\u5143\u7ec4\uff0c\u8fd4\u56de\u7684\u591a\u4e2a\u5217\u8868\u53ef\u4ee5\u901a\u8fc7\u5b57\u6bb5\u540d\u79f0\u8bbf\u95ee\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u6570\u636e\u7c7b\u8fd4\u56de\u591a\u4e2a\u5217\u8868<\/p>\n<\/p>\n<p><p>Python 3.7\u5f15\u5165\u4e86\u6570\u636e\u7c7b\uff08dataclasses\uff09\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u7c7b\u548c\u5904\u7406\u6570\u636e\u3002\u6570\u636e\u7c7b\u53ef\u4ee5\u7528\u4e8e\u8fd4\u56de\u591a\u4e2a\u5217\u8868\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from dataclasses import dataclass<\/p>\n<p>from typing import List<\/p>\n<p>@dataclass<\/p>\n<p>class NumberCategories:<\/p>\n<p>    positives: List[int]<\/p>\n<p>    negatives: List[int]<\/p>\n<p>    zeros: List[int]<\/p>\n<p>def classify_numbers(data):<\/p>\n<p>    positives = []<\/p>\n<p>    negatives = []<\/p>\n<p>    zeros = []<\/p>\n<p>    for num in data:<\/p>\n<p>        if num &gt; 0:<\/p>\n<p>            positives.append(num)<\/p>\n<p>        elif num &lt; 0:<\/p>\n<p>            negatives.append(num)<\/p>\n<p>        else:<\/p>\n<p>            zeros.append(num)<\/p>\n<p>    return NumberCategories(positives, negatives, zeros)<\/p>\n<p>data = [3, -1, 0, 5, -6, 0, 8, -9]<\/p>\n<p>categories = classify_numbers(data)<\/p>\n<p>print(&quot;Positives:&quot;, categories.positives)<\/p>\n<p>print(&quot;Negatives:&quot;, categories.negatives)<\/p>\n<p>print(&quot;Zeros:&quot;, categories.zeros)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>NumberCategories<\/code> \u6570\u636e\u7c7b\u5b9a\u4e49\u4e86\u4e09\u4e2a\u5b57\u6bb5\uff1apositives, negatives \u548c zeros\u3002<code>classify_numbers<\/code> \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a <code>NumberCategories<\/code> \u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u4e09\u4e2a\u5217\u8868\u3002\u6570\u636e\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u4e14\u5177\u5907\u7c7b\u578b\u63d0\u793a\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u548c\u5904\u7406\u591a\u4e2a\u8fd4\u56de\u503c\uff0c\u589e\u5f3a\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8fd4\u56de\u591a\u4e2a\u5217\u8868\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u9009\u62e9\uff0c\u5305\u62ec\u4f7f\u7528\u5143\u7ec4\u3001\u5b57\u5178\u3001\u81ea\u5b9a\u4e49\u7c7b\u3001\u547d\u540d\u5143\u7ec4\u548c\u6570\u636e\u7c7b\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u4f7f\u7528\u5143\u7ec4\u548c\u89e3\u5305\u8fd4\u56de\u503c<\/strong>\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\uff1b\u5b57\u5178\u548c\u547d\u540d\u5143\u7ec4\u63d0\u4f9b\u4e86\u66f4\u6e05\u6670\u7684\u8bbf\u95ee\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u7684\u573a\u666f\uff1b\u81ea\u5b9a\u4e49\u7c7b\u548c\u6570\u636e\u7c7b\u63d0\u4f9b\u4e86\u66f4\u5f3a\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u548c\u64cd\u4f5c\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u8fd8\u80fd\u4f7f\u6570\u636e\u5904\u7406\u66f4\u52a0\u9ad8\u6548\u548c\u6e05\u6670\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5f00\u53d1\u8005\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u5173\u952e\u5728\u4e8e\u786e\u4fdd\u4ee3\u7801\u7684\u6e05\u6670\u6027\u3001\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u8fd4\u56de\u4e24\u4e2a\u5217\u8868\u5e76\u52a0\u4ee5\u533a\u5206\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e24\u4e2a\u5217\u8868\u653e\u5165\u4e00\u4e2a\u5143\u7ec4\u4e2d\u6765\u540c\u65f6\u8fd4\u56de\u5b83\u4eec\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>return list1, list2<\/code>\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u8c03\u7528\u51fd\u6570\u65f6\u5c06\u7ed3\u679c\u89e3\u5305\u4e3a\u4e24\u4e2a\u4e0d\u540c\u7684\u53d8\u91cf\uff0c\u4ece\u800c\u8f7b\u677e\u533a\u5206\u8fd9\u4e24\u4e2a\u5217\u8868\u3002<\/p>\n<p><strong>\u5728\u8fd4\u56de\u7684\u4e24\u4e2a\u5217\u8868\u4e2d\uff0c\u5982\u4f55\u786e\u8ba4\u5b83\u4eec\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u8ba4\u8fd4\u56de\u7684\u4e24\u4e2a\u5217\u8868\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u8c03\u7528\u540e\uff0c\u901a\u8fc7\u6253\u5370\u6216\u5176\u4ed6\u65b9\u5f0f\u68c0\u67e5\u5404\u81ea\u7684\u5185\u5bb9\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>print(list1)<\/code>\u548c<code>print(list2)<\/code>\u53ef\u4ee5\u5206\u522b\u67e5\u770b\u8fd9\u4e24\u4e2a\u5217\u8868\u7684\u5143\u7d20\uff0c\u4fbf\u4e8e\u4e86\u89e3\u6bcf\u4e2a\u5217\u8868\u7684\u5177\u4f53\u5185\u5bb9\u53ca\u5176\u533a\u522b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u5217\u8868\u65f6\u4e3a\u5176\u547d\u540d\u4ee5\u4fbf\u4e8e\u7406\u89e3\uff1f<\/strong><br \/>\u5728\u8fd4\u56de\u4e24\u4e2a\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6709\u610f\u4e49\u7684\u53d8\u91cf\u540d\u79f0\u6765\u589e\u5f3a\u53ef\u8bfb\u6027\u3002\u5728\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5c06\u8fd4\u56de\u7684\u5143\u7ec4\u89e3\u5305\u4e3a\u4e24\u4e2a\u5177\u6709\u63cf\u8ff0\u6027\u540d\u79f0\u7684\u53d8\u91cf\uff0c\u4f8b\u5982<code>even_numbers, odd_numbers = my_function()<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u6e05\u6670\u5730\u7406\u89e3\u6bcf\u4e2a\u5217\u8868\u7684\u7528\u9014\u548c\u542b\u4e49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u65f6\u3002\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u8fd9\u4e9b\u503c\u53ef\u4ee5\u662f\u4e0d [&hellip;]","protected":false},"author":3,"featured_media":1113078,"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\/1113073"}],"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=1113073"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113073\/revisions"}],"predecessor-version":[{"id":1113079,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113073\/revisions\/1113079"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1113078"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1113073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1113073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1113073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}