{"id":1106305,"date":"2025-01-08T16:38:59","date_gmt":"2025-01-08T08:38:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1106305.html"},"modified":"2025-01-08T16:39:01","modified_gmt":"2025-01-08T08:39:01","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e6%95%b0%e5%ad%97%e9%87%91%e5%ad%97%e5%a1%94","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1106305.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u6253\u5370\u6570\u5b57\u91d1\u5b57\u5854"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070826\/ffe3c319-eb94-4cc8-8118-8219cb5e4221.webp\" alt=\"python\u4e2d\u5982\u4f55\u6253\u5370\u6570\u5b57\u91d1\u5b57\u5854\" \/><\/p>\n<p><p> <strong>\u5728 Python \u4e2d\u6253\u5370\u6570\u5b57\u91d1\u5b57\u5854\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u7406\u89e3\u5982\u4f55\u63a7\u5236\u884c\u548c\u5217\u7684\u8f93\u51fa\u3002\u6570\u5b57\u91d1\u5b57\u5854\u7684\u6253\u5370\u4e3b\u8981\u4f9d\u8d56\u4e8e\u884c\u53f7\u548c\u5217\u53f7\u7684\u5173\u7cfb\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5faa\u73af\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u5f62\u6001\u7684\u6570\u5b57\u91d1\u5b57\u5854\uff0c\u4f8b\u5982\u5de6\u5bf9\u9f50\u3001\u53f3\u5bf9\u9f50\u548c\u5c45\u4e2d\u7684\u6570\u5b57\u91d1\u5b57\u5854\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u5728 Python \u4e2d\u6253\u5370\u8fd9\u4e9b\u4e0d\u540c\u5f62\u6001\u7684\u6570\u5b57\u91d1\u5b57\u5854\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3\u6570\u5b57\u91d1\u5b57\u5854\u7684\u57fa\u672c\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6570\u5b57\u91d1\u5b57\u5854\u4e4b\u524d\uff0c\u7406\u89e3\u5176\u57fa\u672c\u7ed3\u6784\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u5b57\u91d1\u5b57\u5854\u770b\u8d77\u6765\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>    1<\/p>\n<p>   1 2 1<\/p>\n<p>  1 2 3 2 1<\/p>\n<p> 1 2 3 4 3 2 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u770b\u51fa\uff0c\u91d1\u5b57\u5854\u7684\u6bcf\u4e00\u5c42\u90fd\u4ee5\u6570\u5b571\u5f00\u59cb\uff0c\u9010\u6e10\u9012\u589e\u5230\u67d0\u4e2a\u6570\u5b57\uff0c\u7136\u540e\u518d\u9012\u51cf\u52301\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u6bcf\u4e00\u5c42\u7684\u7ed3\u6784\u6765\u7f16\u5199\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 Python \u6253\u5370\u7b80\u5355\u7684\u6570\u5b57\u91d1\u5b57\u5854<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u4f7f\u7528\u7b80\u5355\u7684\u5d4c\u5957\u5faa\u73af\u6765\u6253\u5370\u4e00\u4e2a\u6570\u5b57\u91d1\u5b57\u5854\u3002\u4e0b\u9762\u662f\u57fa\u672c\u7684\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_number_pyramid(n):<\/p>\n<p>    for i in range(1, n+1):<\/p>\n<p>        # \u6253\u5370\u524d\u5bfc\u7a7a\u683c<\/p>\n<p>        print(&quot; &quot; * (n - i), end=&quot;&quot;)<\/p>\n<p>        # \u6253\u5370\u9012\u589e\u7684\u6570\u5b57<\/p>\n<p>        for j in range(1, i + 1):<\/p>\n<p>            print(j, end=&quot; &quot;)<\/p>\n<p>        # \u6253\u5370\u9012\u51cf\u7684\u6570\u5b57<\/p>\n<p>        for j in range(i - 1, 0, -1):<\/p>\n<p>            print(j, end=&quot; &quot;)<\/p>\n<p>        # \u6362\u884c<\/p>\n<p>        print()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>n = 5<\/p>\n<p>print_number_pyramid(n)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>print_number_pyramid<\/code>\u51fd\u6570\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u6765\u6253\u5370\u6570\u5b57\u91d1\u5b57\u5854\u3002\u5916\u5c42\u5faa\u73af\u63a7\u5236\u884c\u6570\uff0c\u5185\u5c42\u5faa\u73af\u5206\u522b\u63a7\u5236\u524d\u5bfc\u7a7a\u683c\u3001\u9012\u589e\u548c\u9012\u51cf\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u91ca\u4ee3\u7801\u4e2d\u7684\u5173\u952e\u90e8\u5206<\/h3>\n<\/p>\n<p><h4>1\u3001\u524d\u5bfc\u7a7a\u683c\u7684\u6253\u5370<\/h4>\n<\/p>\n<p><p>\u5728\u91d1\u5b57\u5854\u7684\u6bcf\u4e00\u5c42\u5f00\u59cb\u65f6\uff0c\u9700\u8981\u6253\u5370\u4e00\u5b9a\u6570\u91cf\u7684\u7a7a\u683c\uff0c\u4f7f\u5f97\u6570\u5b57\u5c45\u4e2d\u3002\u7a7a\u683c\u7684\u6570\u91cf\u7531<code>n - i<\/code>\u786e\u5b9a\uff0c\u5176\u4e2d<code>n<\/code>\u662f\u91d1\u5b57\u5854\u7684\u9ad8\u5ea6\uff0c<code>i<\/code>\u662f\u5f53\u524d\u884c\u53f7\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u9012\u589e\u6570\u5b57\u7684\u6253\u5370<\/h4>\n<\/p>\n<p><p>\u5728\u6bcf\u4e00\u884c\u4e2d\uff0c\u9012\u589e\u7684\u6570\u5b57\u4ece1\u5f00\u59cb\u5230\u5f53\u524d\u884c\u53f7<code>i<\/code>\u7ed3\u675f\uff0c\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u6253\u5370\u8fd9\u4e9b\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u9012\u51cf\u6570\u5b57\u7684\u6253\u5370<\/h4>\n<\/p>\n<p><p>\u5728\u9012\u589e\u6570\u5b57\u4e4b\u540e\uff0c\u7d27\u63a5\u7740\u662f\u9012\u51cf\u7684\u6570\u5b57\u3002\u8fd9\u4e9b\u6570\u5b57\u4ece<code>i-1<\/code>\u5f00\u59cb\uff0c\u9012\u51cf\u52301\u3002\u4f7f\u7528\u53e6\u4e00\u4e2a\u5faa\u73af\u6765\u5b9e\u73b0\u8fd9\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6269\u5c55\uff1a\u6253\u5370\u53f3\u5bf9\u9f50\u548c\u5c45\u4e2d\u7684\u6570\u5b57\u91d1\u5b57\u5854<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5c45\u4e2d\u7684\u6570\u5b57\u91d1\u5b57\u5854\uff0c\u8fd8\u53ef\u4ee5\u6253\u5370\u53f3\u5bf9\u9f50\u548c\u5de6\u5bf9\u9f50\u7684\u6570\u5b57\u91d1\u5b57\u5854\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u53f3\u5bf9\u9f50\u7684\u6570\u5b57\u91d1\u5b57\u5854<\/h4>\n<\/p>\n<p><p>\u53f3\u5bf9\u9f50\u7684\u6570\u5b57\u91d1\u5b57\u5854\u53ea\u9700\u8981\u8c03\u6574\u524d\u5bfc\u7a7a\u683c\u7684\u6570\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_right_aligned_pyramid(n):<\/p>\n<p>    for i in range(1, n+1):<\/p>\n<p>        # \u6253\u5370\u524d\u5bfc\u7a7a\u683c<\/p>\n<p>        print(&quot; &quot; * (n - i), end=&quot;&quot;)<\/p>\n<p>        # \u6253\u5370\u6570\u5b57<\/p>\n<p>        for j in range(1, i + 1):<\/p>\n<p>            print(j, end=&quot; &quot;)<\/p>\n<p>        # \u6362\u884c<\/p>\n<p>        print()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>n = 5<\/p>\n<p>print_right_aligned_pyramid(n)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5de6\u5bf9\u9f50\u7684\u6570\u5b57\u91d1\u5b57\u5854<\/h4>\n<\/p>\n<p><p>\u5de6\u5bf9\u9f50\u7684\u6570\u5b57\u91d1\u5b57\u5854\u4e0d\u9700\u8981\u6253\u5370\u524d\u5bfc\u7a7a\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_left_aligned_pyramid(n):<\/p>\n<p>    for i in range(1, n+1):<\/p>\n<p>        # \u6253\u5370\u6570\u5b57<\/p>\n<p>        for j in range(1, i + 1):<\/p>\n<p>            print(j, end=&quot; &quot;)<\/p>\n<p>        # \u6362\u884c<\/p>\n<p>        print()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>n = 5<\/p>\n<p>print_left_aligned_pyramid(n)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u6253\u5370\u6570\u5b57\u91d1\u5b57\u5854\u4e3b\u8981\u4f9d\u8d56\u4e8e\u7406\u89e3\u5176\u57fa\u672c\u7ed3\u6784\u548c\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u63a7\u5236\u8f93\u51fa\u3002\u901a\u8fc7\u8c03\u6574\u524d\u5bfc\u7a7a\u683c\u7684\u6570\u91cf\u548c\u5faa\u73af\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u5f62\u6001\u7684\u6570\u5b57\u91d1\u5b57\u5854\u3002\u638c\u63e1\u8fd9\u4e9b\u57fa\u7840\u77e5\u8bc6\u540e\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u5bf9\u91d1\u5b57\u5854\u7684\u5f62\u6001\u8fdb\u884c\u8c03\u6574\u548c\u6269\u5c55\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u6570\u5b57\u91d1\u5b57\u5854\u7684\u57fa\u672c\u7ed3\u6784\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u6570\u5b57\u91d1\u5b57\u5854\u7684\u57fa\u672c\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3002\u5916\u5c42\u5faa\u73af\u63a7\u5236\u91d1\u5b57\u5854\u7684\u884c\u6570\uff0c\u5185\u5c42\u5faa\u73af\u7528\u4e8e\u6253\u5370\u7a7a\u683c\u548c\u6570\u5b57\u3002\u901a\u8fc7\u8c03\u6574\u7a7a\u683c\u548c\u6570\u5b57\u7684\u6570\u91cf\uff0c\u53ef\u4ee5\u5f62\u6210\u7406\u60f3\u7684\u91d1\u5b57\u5854\u5f62\u72b6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">n = 5  # \u91d1\u5b57\u5854\u7684\u5c42\u6570\nfor i in range(n):\n    print(&#39; &#39; * (n - i - 1) + &#39; &#39;.join(str(num) for num in range(1, i + 2)))\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u5c06\u6253\u5370\u51fa\u4e00\u4e2a\u9ad8\u5ea6\u4e3a5\u7684\u6570\u5b57\u91d1\u5b57\u5854\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u6570\u5b57\u91d1\u5b57\u5854\u7684\u9ad8\u5ea6\u548c\u683c\u5f0f\uff1f<\/strong><br \/>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u53d8\u91cf\u6765\u6539\u53d8\u91d1\u5b57\u5854\u7684\u9ad8\u5ea6\u3002\u4f8b\u5982\uff0c\u4fee\u6539\u53d8\u91cf<code>n<\/code>\u7684\u503c\uff0c\u53ef\u4ee5\u5feb\u901f\u6539\u53d8\u91d1\u5b57\u5854\u7684\u5c42\u6570\u3002\u6b64\u5916\uff0c\u683c\u5f0f\u4e5f\u53ef\u4ee5\u901a\u8fc7\u66f4\u6539\u6253\u5370\u8bed\u53e5\u4e2d\u7684\u7a7a\u683c\u548c\u6570\u5b57\u683c\u5f0f\u6765\u5b9a\u5236\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u6570\u5b57\u6539\u4e3a\u4ece0\u5f00\u59cb\uff0c\u6216\u8c03\u6574\u6570\u5b57\u4e4b\u95f4\u7684\u95f4\u8ddd\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6253\u5370\u6570\u5b57\u91d1\u5b57\u5854\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u9519\u8bef\u9700\u8981\u907f\u514d\uff1f<\/strong><br \/>\u5728\u6253\u5370\u6570\u5b57\u91d1\u5b57\u5854\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u7a7a\u683c\u6570\u91cf\u8ba1\u7b97\u9519\u8bef\u3001\u6570\u5b57\u5faa\u73af\u8303\u56f4\u4e0d\u5bf9\u4ee5\u53ca\u672a\u6b63\u786e\u8bbe\u7f6e\u884c\u6570\u7b49\u3002\u786e\u4fdd\u5916\u5c42\u5faa\u73af\u7684\u8303\u56f4\u6b63\u786e\u8bbe\u7f6e\u4e3a\u91d1\u5b57\u5854\u7684\u5c42\u6570\uff0c\u5e76\u4ed4\u7ec6\u68c0\u67e5\u5185\u5c42\u5faa\u73af\u4e2d\u7684\u7a7a\u683c\u548c\u6570\u5b57\u8f93\u51fa\u903b\u8f91\uff0c\u4ee5\u786e\u4fdd\u91d1\u5b57\u5854\u7684\u5f62\u72b6\u6b63\u786e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\u6253\u5370\u6570\u5b57\u91d1\u5b57\u5854\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u7406\u89e3\u5982\u4f55\u63a7\u5236\u884c\u548c\u5217\u7684\u8f93\u51fa\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1106309,"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\/1106305"}],"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=1106305"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106305\/revisions"}],"predecessor-version":[{"id":1106310,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106305\/revisions\/1106310"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1106309"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1106305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1106305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1106305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}