{"id":1142156,"date":"2025-01-08T22:37:18","date_gmt":"2025-01-08T14:37:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1142156.html"},"modified":"2025-01-08T22:37:20","modified_gmt":"2025-01-08T14:37:20","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e6%89%93%e5%8d%b0%e6%95%b0%e5%ad%97%e6%97%b6%e6%af%8f%e8%a1%8c%e6%89%93%e5%8d%b0%e5%8d%81%e4%b8%aa","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1142156.html","title":{"rendered":"\u5982\u4f55\u5728Python\u6253\u5370\u6570\u5b57\u65f6\u6bcf\u884c\u6253\u5370\u5341\u4e2a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180434\/2a07501a-d3a9-4230-be3d-6cd3c259bdba.webp\" alt=\"\u5982\u4f55\u5728Python\u6253\u5370\u6570\u5b57\u65f6\u6bcf\u884c\u6253\u5370\u5341\u4e2a\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6253\u5370\u6570\u5b57\u65f6\uff0c\u6bcf\u884c\u6253\u5370\u5341\u4e2a\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528for\u5faa\u73af\u4e0e\u6761\u4ef6\u5224\u65ad\u3001\u5217\u8868\u5207\u7247\u3001\u751f\u6210\u5668\u51fd\u6570\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u6700\u76f4\u89c2\u7684\u662f\u4f7f\u7528for\u5faa\u73af\u4e0e\u6761\u4ef6\u5224\u65ad\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff0c\u5e76\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528for\u5faa\u73af\u4e0e\u6761\u4ef6\u5224\u65ad<\/p>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u4e0e\u6761\u4ef6\u5224\u65ad\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u904d\u5386\u4e00\u4e2a\u6570\u5b57\u5217\u8868\uff0c\u5e76\u5728\u6bcf\u6253\u5370\u5341\u4e2a\u6570\u5b57\u540e\u6362\u884c\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6bcf\u884c\u6253\u5370\u5341\u4e2a\u6570\u5b57\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_numbers_per_line(numbers, per_line=10):<\/p>\n<p>    for i, number in enumerate(numbers):<\/p>\n<p>        if (i + 1) % per_line == 0:<\/p>\n<p>            print(number)<\/p>\n<p>        else:<\/p>\n<p>            print(number, end=&#39; &#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u4e86\u4e00\u4e2a\u6570\u5b57\u5217\u8868<code>numbers<\/code>\u3002\u901a\u8fc7\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u7d22\u5f15<code>i<\/code>\u548c\u6570\u5b57<code>number<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u68c0\u6d4b\u5f53\u524d\u7d22\u5f15\u662f\u5426\u662f\u5341\u7684\u500d\u6570\u3002\u5982\u679c\u662f\uff0c\u5219\u5728\u6253\u5370\u540e\u6362\u884c\uff0c\u5426\u5219\u7ee7\u7eed\u6253\u5370\u6570\u5b57\u5e76\u5728\u540e\u9762\u52a0\u4e00\u4e2a\u7a7a\u683c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u5207\u7247<\/p>\n<\/p>\n<p><p>\u5217\u8868\u5207\u7247\u662f\u4e00\u79cd\u66f4Pythonic\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5207\u7247\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5217\u8868\u5206\u5272\u6210\u591a\u4e2a\u5b50\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u5305\u542b\u5341\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u9010\u884c\u6253\u5370\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_numbers_per_line(numbers, per_line=10):<\/p>\n<p>    for i in range(0, len(numbers), per_line):<\/p>\n<p>        print(&#39; &#39;.join(map(str, numbers[i:i+per_line])))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>range<\/code>\u51fd\u6570\u751f\u6210\u5207\u7247\u7684\u8d77\u59cb\u7d22\u5f15\u3002\u6bcf\u6b21\u5207\u7247\u7684\u8303\u56f4\u662f\u4ece<code>i<\/code>\u5230<code>i+per_line<\/code>\u3002\u901a\u8fc7<code>map<\/code>\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528<code>join<\/code>\u51fd\u6570\u5c06\u5b83\u4eec\u8fde\u63a5\u8d77\u6765\uff0c\u518d\u6253\u5370\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u662f\u4e00\u79cd\u9ad8\u7ea7\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002\u901a\u8fc7\u751f\u6210\u5668\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u52a8\u6001\u751f\u6210\u6bcf\u884c\u7684\u6570\u5b57\uff0c\u800c\u4e0d\u9700\u8981\u4e00\u6b21\u6027\u751f\u6210\u6240\u6709\u5207\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_number_lines(numbers, per_line=10):<\/p>\n<p>    for i in range(0, len(numbers), per_line):<\/p>\n<p>        yield numbers[i:i+per_line]<\/p>\n<p>def print_numbers_per_line(numbers, per_line=10):<\/p>\n<p>    for line in generate_number_lines(numbers, per_line):<\/p>\n<p>        print(&#39; &#39;.join(map(str, line)))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>generate_number_lines<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u8fd4\u56de\u6bcf\u884c\u7684\u6570\u5b57\u5217\u8868\u3002<code>print_numbers_per_line<\/code>\u51fd\u6570\u8c03\u7528\u751f\u6210\u5668\u51fd\u6570\uff0c\u5e76\u9010\u884c\u6253\u5370\u751f\u6210\u7684\u6570\u5b57\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u6bd4\u8f83<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>for\u5faa\u73af\u4e0e\u6761\u4ef6\u5224\u65ad<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u6700\u76f4\u89c2\uff0c\u4ee3\u7801\u7b80\u5355\u6613\u61c2\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u3002\u7f3a\u70b9\u662f\u4ee3\u7801\u4e2d\u5305\u542b\u8f83\u591a\u7684\u6761\u4ef6\u5224\u65ad\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5217\u8868\u5207\u7247<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u66f4Pythonic\uff0c\u4ee3\u7801\u7b80\u6d01\u4e14\u53ef\u8bfb\u6027\u9ad8\u3002\u901a\u8fc7\u4f7f\u7528\u5207\u7247\uff0c\u53ef\u4ee5\u907f\u514d\u6761\u4ef6\u5224\u65ad\uff0c\u63d0\u9ad8\u4ee3\u7801\u6027\u80fd\u3002\u9002\u5408\u5904\u7406\u8f83\u5927\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u5668\u51fd\u6570<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u6700\u4e3a\u9ad8\u7ea7\uff0c\u9002\u5408\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u3002\u751f\u6210\u5668\u51fd\u6570\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u52a8\u6001\u751f\u6210\u6570\u636e\uff0c\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\uff0c\u63d0\u9ad8\u6027\u80fd\u3002\u7f3a\u70b9\u662f\u4ee3\u7801\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u4e0d\u9002\u5408\u521d\u5b66\u8005\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u3002\u5982\u679c\u6570\u636e\u89c4\u6a21\u8f83\u5c0f\u4e14\u4ee3\u7801\u9700\u6613\u4e8e\u7406\u89e3\uff0c\u5efa\u8bae\u4f7f\u7528for\u5faa\u73af\u4e0e\u6761\u4ef6\u5224\u65ad\u7684\u65b9\u6cd5\u3002\u5982\u679c\u6570\u636e\u89c4\u6a21\u8f83\u5927\u4e14\u9700\u8981\u9ad8\u6027\u80fd\uff0c\u5efa\u8bae\u4f7f\u7528\u5217\u8868\u5207\u7247\u6216\u751f\u6210\u5668\u51fd\u6570\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u6269\u5c55\u529f\u80fd<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u81ea\u5b9a\u4e49\u6bcf\u884c\u6253\u5370\u7684\u6570\u5b57\u6570\u91cf\u3001\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff08\u5982\u5b57\u7b26\u4e32\uff09\u3001\u5728\u6bcf\u884c\u524d\u540e\u6dfb\u52a0\u7279\u5b9a\u5b57\u7b26\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u6269\u5c55\u7248\u7684\u51fd\u6570\uff0c\u63d0\u4f9b\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_custom_numbers_per_line(numbers, per_line=10, separator=&#39; &#39;, prefix=&#39;&#39;, suffix=&#39;&#39;):<\/p>\n<p>    for i in range(0, len(numbers), per_line):<\/p>\n<p>        line = prefix + separator.join(map(str, numbers[i:i+per_line])) + suffix<\/p>\n<p>        print(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c<code>per_line<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u6bcf\u884c\u6253\u5370\u7684\u6570\u5b57\u6570\u91cf\uff0c<code>separator<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u6570\u5b57\u4e4b\u95f4\u7684\u5206\u9694\u7b26\uff0c<code>prefix<\/code>\u548c<code>suffix<\/code>\u53c2\u6570\u7528\u4e8e\u5728\u6bcf\u884c\u524d\u540e\u6dfb\u52a0\u7279\u5b9a\u5b57\u7b26\u3002\u901a\u8fc7\u8fd9\u4e9b\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5b9a\u5236\u8f93\u51fa\u683c\u5f0f\uff0c\u6ee1\u8db3\u4e0d\u540c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6253\u5370\u6570\u5b57\u65f6\u6bcf\u884c\u6253\u5370\u5341\u4e2a\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u4f7f\u7528for\u5faa\u73af\u4e0e\u6761\u4ef6\u5224\u65ad\u3001\u5217\u8868\u5207\u7247\u3001\u751f\u6210\u5668\u51fd\u6570\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u3002\u901a\u8fc7\u6269\u5c55\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u6ee1\u8db3\u4e0d\u540c\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u5176\u539f\u7406\u548c\u5e94\u7528\u573a\u666f\u90fd\u662f\u5173\u952e\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u505a\u51fa\u66f4\u597d\u7684\u51b3\u7b56\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6bcf\u884c\u6253\u5370\u5341\u4e2a\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u6765\u5b9e\u73b0\u6bcf\u884c\u6253\u5370\u5341\u4e2a\u6570\u5b57\u3002\u901a\u8fc7\u8bbe\u7f6e\u8ba1\u6570\u5668\uff0c\u60a8\u53ef\u4ee5\u5728\u6bcf\u5341\u4e2a\u6570\u5b57\u540e\u63d2\u5165\u6362\u884c\u7b26\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(1, 101):  # \u6253\u5370\u4ece1\u5230100\u7684\u6570\u5b57\n    print(i, end=&#39; &#39;)\n    if i % 10 == 0:  # \u6bcf\u5341\u4e2a\u6570\u5b57\u6362\u884c\n        print()\n<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u6570\u5b57\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u65b9\u6cd5\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528f-string\u3001<code>format()<\/code>\u65b9\u6cd5\u6216\u8005\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3002\u6bd4\u5982\uff0c\u4f7f\u7528f-string\u683c\u5f0f\u5316\u8f93\u51fa\u6570\u5b57\uff0c\u53ef\u4ee5\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\u6216\u8005\u6dfb\u52a0\u524d\u5bfc\u96f6\u3002\u8fd9\u6709\u52a9\u4e8e\u5728\u6253\u5370\u65f6\u63d0\u5347\u53ef\u8bfb\u6027\u3002<\/p>\n<pre><code class=\"language-python\">for i in range(1, 101):\n    print(f&quot;{i:02}&quot;, end=&#39; &#39;)  # \u4ee5\u4e24\u4f4d\u6570\u5b57\u683c\u5f0f\u6253\u5370\n    if i % 10 == 0:\n        print()\n<\/code><\/pre>\n<p><strong>\u5728\u6253\u5370\u7684\u6570\u5b57\u4e2d\u5982\u4f55\u6dfb\u52a0\u5176\u4ed6\u4fe1\u606f\uff0c\u6bd4\u5982\u5e8f\u53f7\u6216\u6807\u7b7e\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u5728\u6253\u5370\u6570\u5b57\u65f6\u6dfb\u52a0\u6807\u7b7e\u6216\u5e8f\u53f7\uff0c\u901a\u8fc7\u5728print\u51fd\u6570\u4e2d\u62fc\u63a5\u5b57\u7b26\u4e32\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728\u6bcf\u4e2a\u6570\u5b57\u524d\u52a0\u4e0a\u201c\u7f16\u53f7\uff1a\u201d\u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u6587\u672c\u3002<\/p>\n<pre><code class=\"language-python\">for i in range(1, 101):\n    print(f&quot;\u7f16\u53f7\uff1a{i}&quot;, end=&#39; &#39;)\n    if i % 10 == 0:\n        print()\n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u65b9\u5f0f\u4e0d\u4ec5\u80fd\u6ee1\u8db3\u6bcf\u884c\u6253\u5370\u5341\u4e2a\u6570\u5b57\u7684\u9700\u6c42\uff0c\u8fd8\u80fd\u4f7f\u8f93\u51fa\u66f4\u5177\u4fe1\u606f\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6253\u5370\u6570\u5b57\u65f6\uff0c\u6bcf\u884c\u6253\u5370\u5341\u4e2a\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528for\u5faa\u73af\u4e0e\u6761\u4ef6\u5224\u65ad\u3001\u5217\u8868\u5207\u7247\u3001\u751f\u6210\u5668\u51fd\u6570\u3002 \u5728\u8fd9\u4e9b\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1142165,"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\/1142156"}],"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=1142156"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142156\/revisions"}],"predecessor-version":[{"id":1142168,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1142156\/revisions\/1142168"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1142165"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1142156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1142156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1142156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}