{"id":1161548,"date":"2025-01-13T19:17:45","date_gmt":"2025-01-13T11:17:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161548.html"},"modified":"2025-01-13T19:17:48","modified_gmt":"2025-01-13T11:17:48","slug":"%e5%a6%82%e4%bd%95%e5%86%99%e5%87%ba%e4%bc%98%e9%9b%85%e7%9a%84python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161548.html","title":{"rendered":"\u5982\u4f55\u5199\u51fa\u4f18\u96c5\u7684python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202755\/7643eae9-b368-45ed-ba9e-d17c2713606e.webp\" alt=\"\u5982\u4f55\u5199\u51fa\u4f18\u96c5\u7684python\" \/><\/p>\n<p><p> <strong>\u5199\u51fa\u4f18\u96c5\u7684Python\u4ee3\u7801\u9700\u8981\u9075\u5faa\u4e00\u4e9b\u57fa\u672c\u539f\u5219\uff1a\u4ee3\u7801\u7b80\u6d01\u3001\u53ef\u8bfb\u6027\u9ad8\u3001\u9075\u5faaPEP 8\u89c4\u8303\u3001\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u3001\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u3001\u6ce8\u91cd\u5f02\u5e38\u5904\u7406\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u4ee3\u7801\u7684\u53ef\u8bfb\u6027<\/strong>\u662f\u6700\u91cd\u8981\u7684\u4e00\u70b9\uff0c\u56e0\u4e3a\u53ef\u8bfb\u7684\u4ee3\u7801\u4e0d\u4ec5\u65b9\u4fbf\u81ea\u5df1\u540e\u7eed\u7ef4\u62a4\uff0c\u4e5f\u65b9\u4fbf\u4ed6\u4eba\u7406\u89e3\u548c\u6539\u8fdb\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u5165\u624b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u6709\u610f\u4e49\u7684\u53d8\u91cf\u548c\u51fd\u6570\u540d\u79f0<\/strong>\uff1a\u53d8\u91cf\u540d\u548c\u51fd\u6570\u540d\u5e94\u8be5\u80fd\u591f\u6e05\u6670\u5730\u8868\u8fbe\u5b83\u4eec\u7684\u7528\u9014\u3002<\/li>\n<li><strong>\u9075\u5faaPEP 8\u89c4\u8303<\/strong>\uff1aPEP 8\u662fPython\u7684\u7f16\u7801\u89c4\u8303\uff0c\u9075\u5faa\u8fd9\u4e9b\u89c4\u8303\u53ef\u4ee5\u4f7f\u4ee3\u7801\u98ce\u683c\u4e00\u81f4\uff0c\u66f4\u5bb9\u6613\u7406\u89e3\u3002<\/li>\n<li><strong>\u907f\u514d\u8fc7\u4e8e\u590d\u6742\u7684\u8868\u8fbe\u5f0f<\/strong>\uff1a\u5c06\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u62c6\u5206\u6210\u7b80\u5355\u7684\u90e8\u5206\uff0c\u4f7f\u7528\u4e2d\u95f4\u53d8\u91cf\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6ce8\u91ca\u548c\u6587\u6863\u5b57\u7b26\u4e32<\/strong>\uff1a\u9002\u5f53\u7684\u6ce8\u91ca\u548c\u6587\u6863\u5b57\u7b26\u4e32\u53ef\u4ee5\u89e3\u91ca\u4ee3\u7801\u7684\u76ee\u7684\u548c\u7528\u6cd5\u3002<\/li>\n<li><strong>\u6a21\u5757\u5316\u548c\u51fd\u6570\u5316<\/strong>\uff1a\u5c06\u4ee3\u7801\u5206\u89e3\u6210\u72ec\u7acb\u7684\u6a21\u5757\u548c\u51fd\u6570\uff0c\u6bcf\u4e2a\u51fd\u6570\u53ea\u505a\u4e00\u4ef6\u4e8b\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u9762\u7684\u4f18\u96c5\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528\u6709\u610f\u4e49\u7684\u53d8\u91cf\u548c\u51fd\u6570\u540d\u79f0<\/strong><\/h2>\n<p><p><strong>1. \u53d8\u91cf\u540d\u79f0<\/strong><\/p>\n<\/p>\n<p><p>\u9009\u62e9\u6709\u610f\u4e49\u7684\u53d8\u91cf\u540d\u79f0\u662f\u7f16\u5199\u53ef\u8bfb\u4ee3\u7801\u7684\u7b2c\u4e00\u6b65\u3002\u53d8\u91cf\u540d\u79f0\u5e94\u8be5\u6e05\u695a\u5730\u63cf\u8ff0\u5b83\u4eec\u6240\u5b58\u50a8\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>total_price<\/code>\u800c\u4e0d\u662f<code>tp<\/code>\uff0c\u4f7f\u7528<code>user_age<\/code>\u800c\u4e0d\u662f<code>ua<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u5728\u9605\u8bfb\u4ee3\u7801\u65f6\uff0c\u4e0d\u9700\u8981\u82b1\u65f6\u95f4\u53bb\u731c\u6d4b\u53d8\u91cf\u7684\u542b\u4e49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>tp = 100<\/p>\n<p>ua = 25<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>total_price = 100<\/p>\n<p>user_age = 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u51fd\u6570\u540d\u79f0<\/strong><\/p>\n<\/p>\n<p><p>\u51fd\u6570\u540d\u79f0\u540c\u6837\u91cd\u8981\uff0c\u4e00\u4e2a\u597d\u7684\u51fd\u6570\u540d\u79f0\u5e94\u8be5\u63cf\u8ff0\u51fd\u6570\u7684\u884c\u4e3a\u6216\u4f5c\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>calculate_total_price<\/code>\u800c\u4e0d\u662f<code>ctp<\/code>\uff0c\u4f7f\u7528<code>get_user_age<\/code>\u800c\u4e0d\u662f<code>gua<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>def ctp(items):<\/p>\n<p>    total = 0<\/p>\n<p>    for item in items:<\/p>\n<p>        total += item.price<\/p>\n<p>    return total<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>def calculate_total_price(items):<\/p>\n<p>    total = 0<\/p>\n<p>    for item in items:<\/p>\n<p>        total += item.price<\/p>\n<p>    return total<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u9075\u5faaPEP 8\u89c4\u8303<\/strong><\/h2>\n<p><p>PEP 8\u662fPython\u7684\u7f16\u7801\u89c4\u8303\uff0c\u9075\u5faa\u8fd9\u4e9b\u89c4\u8303\u53ef\u4ee5\u4f7f\u4ee3\u7801\u98ce\u683c\u4e00\u81f4\uff0c\u66f4\u5bb9\u6613\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u7f29\u8fdb<\/strong><\/p>\n<\/p>\n<p><p>PEP 8\u5efa\u8bae\u4f7f\u75284\u4e2a\u7a7a\u683c\u8fdb\u884c\u7f29\u8fdb\uff0c\u800c\u4e0d\u662f\u4f7f\u7528Tab\u952e\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u4e0d\u540c\u7f16\u8f91\u5668\u548cIDE\u5bf9Tab\u952e\u5904\u7406\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>def calculate_total_price(items):<\/p>\n<p>  total = 0<\/p>\n<p>  for item in items:<\/p>\n<p>      total += item.price<\/p>\n<p>  return total<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>def calculate_total_price(items):<\/p>\n<p>    total = 0<\/p>\n<p>    for item in items:<\/p>\n<p>        total += item.price<\/p>\n<p>    return total<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u7a7a\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u51fd\u6570\u548c\u7c7b\u4e4b\u95f4\u5e94\u8be5\u4f7f\u7528\u4e24\u4e2a\u7a7a\u884c\u8fdb\u884c\u5206\u9694\uff0c\u51fd\u6570\u5185\u90e8\u7684\u4ee3\u7801\u5757\u4e4b\u95f4\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7a7a\u884c\u5206\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>def function_one():<\/p>\n<p>    pass<\/p>\n<p>def function_two():<\/p>\n<p>    pass<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>def function_one():<\/p>\n<p>    pass<\/p>\n<p>def function_two():<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u884c\u957f\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>\u6bcf\u884c\u4ee3\u7801\u7684\u957f\u5ea6\u4e0d\u5e94\u8be5\u8d85\u8fc779\u4e2a\u5b57\u7b26\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u5927\u591a\u6570\u7f16\u8f91\u5668\u548cIDE\u4e2d\u65b9\u4fbf\u67e5\u770b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>def calculate_total_price(items):<\/p>\n<p>    total = 0<\/p>\n<p>    for item in items:<\/p>\n<p>        total += item.price<\/p>\n<p>        if total &gt; 100:<\/p>\n<p>            break<\/p>\n<p>    return total<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>def calculate_total_price(items):<\/p>\n<p>    total = 0<\/p>\n<p>    for item in items:<\/p>\n<p>        total += item.price<\/p>\n<p>        if total &gt; 100:<\/p>\n<p>            break<\/p>\n<p>    return total<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u907f\u514d\u8fc7\u4e8e\u590d\u6742\u7684\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p><p>\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u4f1a\u4f7f\u4ee3\u7801\u96be\u4ee5\u7406\u89e3\u548c\u7ef4\u62a4\u3002\u53ef\u4ee5\u901a\u8fc7\u62c6\u5206\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u4e2d\u95f4\u53d8\u91cf\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>result = (a + b) * (c - d) \/ (e + f)<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>sum_ab = a + b<\/p>\n<p>diff_cd = c - d<\/p>\n<p>sum_ef = e + f<\/p>\n<p>result = sum_ab * diff_cd \/ sum_ef<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528\u6ce8\u91ca\u548c\u6587\u6863\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p><p><strong>1. \u6ce8\u91ca<\/strong><\/p>\n<\/p>\n<p><p>\u6ce8\u91ca\u53ef\u4ee5\u5e2e\u52a9\u89e3\u91ca\u4ee3\u7801\u7684\u76ee\u7684\u548c\u7528\u6cd5\uff0c\u4f46\u4e0d\u5e94\u8be5\u8fc7\u5ea6\u4f7f\u7528\u3002\u6ce8\u91ca\u5e94\u8be5\u7b80\u6d01\u660e\u4e86\uff0c\u653e\u5728\u4ee3\u7801\u7684\u4e0a\u65b9\u6216\u65c1\u8fb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>x = 10  # \u8fd9\u662fx\u53d8\u91cf<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<h2><strong>\u8bbe\u7f6ex\u53d8\u91cf\u7684\u521d\u59cb\u503c\u4e3a10<\/strong><\/h2>\n<p>x = 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u6587\u6863\u5b57\u7b26\u4e32<\/strong><\/p>\n<\/p>\n<p><p>\u6587\u6863\u5b57\u7b26\u4e32\u7528\u4e8e\u89e3\u91ca\u6a21\u5757\u3001\u7c7b\u548c\u51fd\u6570\u7684\u7528\u9014\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e94\u8be5\u653e\u5728\u5b9a\u4e49\u7684\u7b2c\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c\u3002<\/p>\n<p>    \u53c2\u6570:<\/p>\n<p>    a (int): \u7b2c\u4e00\u4e2a\u6570<\/p>\n<p>    b (int): \u7b2c\u4e8c\u4e2a\u6570<\/p>\n<p>    \u8fd4\u56de:<\/p>\n<p>    int: \u4e24\u4e2a\u6570\u7684\u548c<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u6a21\u5757\u5316\u548c\u51fd\u6570\u5316<\/strong><\/h2>\n<p><p>\u5c06\u4ee3\u7801\u5206\u89e3\u6210\u72ec\u7acb\u7684\u6a21\u5757\u548c\u51fd\u6570\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u6bcf\u4e2a\u51fd\u6570\u53ea\u505a\u4e00\u4ef6\u4e8b\uff0c\u6a21\u5757\u4e4b\u95f4\u901a\u8fc7\u63a5\u53e3\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u51fd\u6570\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u903b\u8f91\u5206\u89e3\u6210\u72ec\u7acb\u7684\u51fd\u6570\uff0c\u6bcf\u4e2a\u51fd\u6570\u53ea\u505a\u4e00\u4ef6\u4e8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>def process_data(data):<\/p>\n<p>    # \u5904\u7406\u6570\u636e<\/p>\n<p>    processed_data = []<\/p>\n<p>    for item in data:<\/p>\n<p>        processed_data.append(item * 2)<\/p>\n<p>    # \u8ba1\u7b97\u5e73\u5747\u503c<\/p>\n<p>    total = sum(processed_data)<\/p>\n<p>    avg = total \/ len(processed_data)<\/p>\n<p>    return avg<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>def process_data(data):<\/p>\n<p>    processed_data = process_items(data)<\/p>\n<p>    avg = calculate_average(processed_data)<\/p>\n<p>    return avg<\/p>\n<p>def process_items(data):<\/p>\n<p>    return [item * 2 for item in data]<\/p>\n<p>def calculate_average(data):<\/p>\n<p>    total = sum(data)<\/p>\n<p>    return total \/ len(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u6a21\u5757\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u76f8\u5173\u7684\u529f\u80fd\u653e\u5728\u540c\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u901a\u8fc7\u63a5\u53e3\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>def read_data(filename):<\/p>\n<p>    # \u8bfb\u53d6\u6587\u4ef6<\/p>\n<p>    with open(filename, &#39;r&#39;) as file:<\/p>\n<p>        data = file.read()<\/p>\n<p>    return data<\/p>\n<p>def process_data(data):<\/p>\n<p>    # \u5904\u7406\u6570\u636e<\/p>\n<p>    processed_data = []<\/p>\n<p>    for item in data:<\/p>\n<p>        processed_data.append(item * 2)<\/p>\n<p>    return processed_data<\/p>\n<p>def save_data(filename, data):<\/p>\n<p>    # \u4fdd\u5b58\u6570\u636e<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>import data_reader<\/p>\n<p>import data_processor<\/p>\n<p>import data_saver<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    data = data_reader.read_data(&#39;input.txt&#39;)<\/p>\n<p>    processed_data = data_processor.process_data(data)<\/p>\n<p>    data_saver.save_data(&#39;output.txt&#39;, processed_data)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u57fa\u672c\u539f\u5219\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u4f18\u96c5\u7684Python\u4ee3\u7801\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5<\/strong><\/h2>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u662f\u7f16\u5199\u9ad8\u6548\u4ee3\u7801\u7684\u5173\u952e\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u5217\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u662f\u4e00\u79cd\u6709\u5e8f\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u5b58\u50a8\u4efb\u610f\u7c7b\u578b\u7684\u5143\u7d20\u3002\u5217\u8868\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5982<code>append()<\/code>\u3001<code>extend()<\/code>\u3001<code>insert()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>a = [1, 2, 3]<\/p>\n<p>a[len(a):] = [4, 5, 6]<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>a = [1, 2, 3]<\/p>\n<p>a.extend([4, 5, 6])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u5b57\u5178<\/strong><\/p>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u96c6\u5408\uff0c\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u50a8\u6570\u636e\u3002\u5b57\u5178\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5982<code>get()<\/code>\u3001<code>items()<\/code>\u3001<code>keys()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>d = {}<\/p>\n<p>d[&#39;a&#39;] = 1<\/p>\n<p>d[&#39;b&#39;] = 2<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>d = {&#39;a&#39;: 1, &#39;b&#39;: 2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u96c6\u5408<\/strong><\/p>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u96c6\u5408\uff0c\u4e0d\u5141\u8bb8\u91cd\u590d\u7684\u5143\u7d20\u3002\u96c6\u5408\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5982<code>add()<\/code>\u3001<code>remove()<\/code>\u3001<code>union()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>s = set()<\/p>\n<p>s.add(1)<\/p>\n<p>s.add(2)<\/p>\n<p>s.add(3)<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>s = {1, 2, 3}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001\u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/strong><\/h2>\n<p><p>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u53ef\u9760\u6027\u3002\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u5e2e\u52a9\u53d1\u73b0\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u4ee3\u7801\u5728\u4fee\u6539\u540e\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528unittest\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684unittest\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u53ef\u4ee5\u7f16\u5199\u548c\u8fd0\u884c\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>class TestAdd(unittest.TestCase):<\/p>\n<p>    def test_add(self):<\/p>\n<p>        self.assertEqual(add(1, 2), 3)<\/p>\n<p>        self.assertEqual(add(-1, 1), 0)<\/p>\n<p>        self.assertEqual(add(0, 0), 0)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528pytest\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p>pytest\u662f\u4e00\u4e2a\u66f4\u4e3a\u7b80\u6d01\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u53c2\u6570\u5316\u6d4b\u8bd5\u3001\u6d4b\u8bd5\u5939\u5177\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pytest<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def test_add():<\/p>\n<p>    assert add(1, 2) == 3<\/p>\n<p>    assert add(-1, 1) == 0<\/p>\n<p>    assert add(0, 0) == 0<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    pytest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516b\u3001\u6ce8\u91cd\u5f02\u5e38\u5904\u7406<\/strong><\/h2>\n<p><p>\u5f02\u5e38\u5904\u7406\u662f\u7f16\u5199\u5065\u58ee\u4ee3\u7801\u7684\u91cd\u8981\u90e8\u5206\u3002\u901a\u8fc7\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5bb9\u9519\u6027\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528try-except\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p>try-except\u8bed\u53e5\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u5f02\u5e38\u7c7b\u578b\uff0c\u63d0\u4f9b\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>def divide(a, b):<\/p>\n<p>    return a \/ b<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>def divide(a, b):<\/p>\n<p>    try:<\/p>\n<p>        return a \/ b<\/p>\n<p>    except ZeroDivisionError:<\/p>\n<p>        return &#39;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528finally\u5b50\u53e5<\/strong><\/p>\n<\/p>\n<p><p>finally\u5b50\u53e5\u7528\u4e8e\u5728\u5f02\u5e38\u5904\u7406\u7ed3\u675f\u540e\uff0c\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>def read_file(filename):<\/p>\n<p>    file = open(filename, &#39;r&#39;)<\/p>\n<p>    data = file.read()<\/p>\n<p>    file.close()<\/p>\n<p>    return data<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>def read_file(filename):<\/p>\n<p>    try:<\/p>\n<p>        file = open(filename, &#39;r&#39;)<\/p>\n<p>        data = file.read()<\/p>\n<p>    except IOError:<\/p>\n<p>        return &#39;\u6587\u4ef6\u8bfb\u53d6\u9519\u8bef&#39;<\/p>\n<p>    finally:<\/p>\n<p>        file.close()<\/p>\n<p>    return data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/p>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u5f02\u5e38\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u7ee7\u627fException\u7c7b\uff0c\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>def validate_age(age):<\/p>\n<p>    if age &lt; 0:<\/p>\n<p>        raise ValueError(&#39;\u5e74\u9f84\u4e0d\u80fd\u4e3a\u8d1f\u6570&#39;)<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>class InvalidAgeError(Exception):<\/p>\n<p>    pass<\/p>\n<p>def validate_age(age):<\/p>\n<p>    if age &lt; 0:<\/p>\n<p>        raise InvalidAgeError(&#39;\u5e74\u9f84\u4e0d\u80fd\u4e3a\u8d1f\u6570&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e5d\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong><\/h2>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u7684\u5206\u914d\u548c\u91ca\u653e\uff0c\u4f8b\u5982\u6587\u4ef6\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u3002\u901a\u8fc7\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u7b80\u5316<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u7684\u4ee3\u7801\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528with\u8bed\u53e5<\/strong><\/p>\n<\/p>\n<p><p>with\u8bed\u53e5\u7528\u4e8e\u7b80\u5316\u8d44\u6e90\u7ba1\u7406\u7684\u4ee3\u7801\uff0c\u786e\u4fdd\u8d44\u6e90\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u81ea\u52a8\u91ca\u653e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>file = open(&#39;example.txt&#39;, &#39;r&#39;)<\/p>\n<p>data = file.read()<\/p>\n<p>file.close()<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__enter__<\/code>\u548c<code>__exit__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>class Resource:<\/p>\n<p>    def open(self):<\/p>\n<p>        print(&#39;\u8d44\u6e90\u6253\u5f00&#39;)<\/p>\n<p>    def close(self):<\/p>\n<p>        print(&#39;\u8d44\u6e90\u5173\u95ed&#39;)<\/p>\n<p>resource = Resource()<\/p>\n<p>resource.open()<\/p>\n<h2><strong>\u4f7f\u7528\u8d44\u6e90<\/strong><\/h2>\n<p>resource.close()<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>class Resource:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(&#39;\u8d44\u6e90\u6253\u5f00&#39;)<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_val, exc_tb):<\/p>\n<p>        print(&#39;\u8d44\u6e90\u5173\u95ed&#39;)<\/p>\n<p>with Resource() as resource:<\/p>\n<p>    # \u4f7f\u7528\u8d44\u6e90<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u3001\u4f7f\u7528\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668<\/strong><\/h2>\n<p><p>\u751f\u6210\u5668\u548c\u8fed\u4ee3\u5668\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002\u751f\u6210\u5668\u7528\u4e8e\u751f\u6210\u4e00\u7cfb\u5217\u503c\uff0c\u800c\u4e0d\u9700\u8981\u4e00\u6b21\u6027\u5c06\u6240\u6709\u503c\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u8fed\u4ee3\u5668\u7528\u4e8e\u8fed\u4ee3\u8bbf\u95ee\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528\u751f\u6210\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u751f\u6210\u503c\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u751f\u6210\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>def generate_numbers(n):<\/p>\n<p>    result = []<\/p>\n<p>    for i in range(n):<\/p>\n<p>        result.append(i)<\/p>\n<p>    return result<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>def generate_numbers(n):<\/p>\n<p>    for i in range(n):<\/p>\n<p>        yield i<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528\u8fed\u4ee3\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u8fed\u4ee3\u5668\u5b9e\u73b0\u4e86<code>__iter__<\/code>\u548c<code>__next__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8fed\u4ee3\u8bbf\u95ee\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u4f18\u96c5\u7684\u4ee3\u7801<\/p>\n<p>class MyIterator:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>        self.index = 0<\/p>\n<p>    def __iter__(self):<\/p>\n<p>        return self<\/p>\n<p>    def __next__(self):<\/p>\n<p>        if self.index &gt;= len(self.data):<\/p>\n<p>            raise StopIteration<\/p>\n<p>        result = self.data[self.index]<\/p>\n<p>        self.index += 1<\/p>\n<p>        return result<\/p>\n<p>data = [1, 2, 3]<\/p>\n<p>iterator = MyIterator(data)<\/p>\n<p>for item in iterator:<\/p>\n<p>    print(item)<\/p>\n<h2><strong>\u4f18\u96c5\u7684\u4ee3\u7801<\/strong><\/h2>\n<p>class MyIterable:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>    def __iter__(self):<\/p>\n<p>        for item in self.data:<\/p>\n<p>            yield item<\/p>\n<p>data = [1, 2, 3]<\/p>\n<p>iterable = MyIterable(data)<\/p>\n<p>for item in iterable:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u57fa\u672c\u539f\u5219\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u4f18\u96c5\u7684Python\u4ee3\u7801\u3002\u8fd9\u6837\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002\u5e0c\u671b\u8fd9\u4e9b\u6280\u5de7\u548c\u5efa\u8bae\u80fd\u591f\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u547d\u540d\u98ce\u683c\u6765\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff1f<\/strong><br \/>\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u547d\u540d\u98ce\u683c\u81f3\u5173\u91cd\u8981\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\uff08snake_case\uff09\u6765\u547d\u540d\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u800c\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\uff08CamelCase\uff09\u6765\u547d\u540d\u7c7b\u3002\u8fd9\u6837\u7684\u547d\u540d\u65b9\u5f0f\u4e0d\u4ec5\u80fd\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u80fd\u5e2e\u52a9\u56e2\u961f\u6210\u5458\u5feb\u901f\u7406\u89e3\u4ee3\u7801\u7684\u529f\u80fd\u548c\u7ed3\u6784\u3002\u6b64\u5916\uff0c\u4fdd\u6301\u547d\u540d\u7684\u4e00\u81f4\u6027\uff0c\u907f\u514d\u4f7f\u7528\u7f29\u5199\u548c\u4e0d\u660e\u786e\u7684\u8bcd\u6c47\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u4f18\u96c5\u7a0b\u5ea6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u6709\u6548\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u7b80\u5316\u4ee3\u7801\uff1f<\/strong><br \/>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684Python\u7279\u6027\uff0c\u53ef\u4ee5\u8ba9\u4f60\u7684\u4ee3\u7801\u66f4\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\u3002\u901a\u8fc7\u5c06\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u5408\u5e76\u5230\u4e00\u884c\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u80fd\u591f\u5feb\u901f\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5e73\u65b9\u6570\u7684\u5217\u8868\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528\u5197\u957f\u7684for\u5faa\u73af\u3002\u8fd9\u79cd\u7b80\u5316\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u66f4\u4f18\u96c5\uff0c\u8fd8\u80fd\u63d0\u9ad8\u8fd0\u884c\u6548\u7387\uff0c\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5229\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u63d0\u5347\u4ee3\u7801\u7684\u4f18\u96c5\u6027\u548c\u6548\u7387\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\uff0c\u5982<code>map()<\/code>\u3001<code>filter()<\/code>\u548c<code>reduce()<\/code>\uff0c\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4ee5\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u5904\u7406\u6570\u636e\u3002\u8fd9\u4e9b\u51fd\u6570\u80fd\u591f\u907f\u514d\u4f7f\u7528\u663e\u5f0f\u7684\u5faa\u73af\uff0c\u4ece\u800c\u51cf\u5c11\u4ee3\u7801\u7684\u590d\u6742\u6027\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>filter()<\/code>\u53ef\u4ee5\u5feb\u901f\u7b5b\u9009\u51fa\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u800c\u4f7f\u7528<code>map()<\/code>\u5219\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u51fd\u6570\u3002\u8fd9\u4e9b\u5185\u7f6e\u51fd\u6570\u7684\u5408\u7406\u8fd0\u7528\uff0c\u4e0d\u4ec5\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u4f18\u96c5\u6027\uff0c\u8fd8\u80fd\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5199\u51fa\u4f18\u96c5\u7684Python\u4ee3\u7801\u9700\u8981\u9075\u5faa\u4e00\u4e9b\u57fa\u672c\u539f\u5219\uff1a\u4ee3\u7801\u7b80\u6d01\u3001\u53ef\u8bfb\u6027\u9ad8\u3001\u9075\u5faaPEP 8\u89c4\u8303\u3001\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97 [&hellip;]","protected":false},"author":3,"featured_media":1161558,"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\/1161548"}],"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=1161548"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161548\/revisions"}],"predecessor-version":[{"id":1161561,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161548\/revisions\/1161561"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161558"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}