{"id":1022834,"date":"2024-12-27T13:38:48","date_gmt":"2024-12-27T05:38:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1022834.html"},"modified":"2024-12-27T13:38:50","modified_gmt":"2024-12-27T05:38:50","slug":"%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e6%95%b4%e5%9e%8b%e5%88%97%e8%a1%a8python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1022834.html","title":{"rendered":"\u5982\u4f55\u521b\u5efa\u6574\u578b\u5217\u8868python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25165256\/3b08e816-ada0-4554-9e10-c9b1b9b47413.webp\" alt=\"\u5982\u4f55\u521b\u5efa\u6574\u578b\u5217\u8868python\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u521b\u5efa\u6574\u578b\u5217\u8868\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u5229\u7528range\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5668<\/strong>\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u76f4\u63a5\u8d4b\u503c\u548c\u5229\u7528range\u51fd\u6570\u521b\u5efa\u8fde\u7eed\u6574\u6570\u5217\u8868\u3002\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u624b\u52a8\u5b9a\u4e49\u7684\u5c11\u91cf\u6574\u6570\u5217\u8868\uff1b\u800c\u4f7f\u7528range\u51fd\u6570\u5219\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u6574\u6570\u5e8f\u5217\uff0c\u975e\u5e38\u9ad8\u6548\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u4e2a\u4eba\u7ecf\u9a8c\u89c1\u89e3\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c\u521b\u5efa\u6574\u578b\u5217\u8868<\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u662f\u521b\u5efa\u6574\u578b\u5217\u8868\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u4f60\u53ea\u9700\u5c06\u6574\u6570\u503c\u653e\u5165\u65b9\u62ec\u53f7\u4e2d\uff0c\u5e76\u7528\u9017\u53f7\u5206\u9694\u5373\u53ef\u3002\u6b64\u65b9\u6cd5\u9002\u7528\u4e8e\u5217\u8868\u5185\u5bb9\u5df2\u77e5\u4e14\u8f83\u77ed\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u76f4\u63a5\u8d4b\u503c\u521b\u5efa\u6574\u578b\u5217\u8868<\/p>\n<p>int_list = [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u4f46\u5f53\u5217\u8868\u5185\u5bb9\u8f83\u957f\u6216\u9700\u8981\u52a8\u6001\u751f\u6210\u65f6\uff0c\u6548\u7387\u8f83\u4f4e\u3002\u4e2a\u4eba\u5efa\u8bae\u5728\u660e\u786e\u5df2\u77e5\u6570\u503c\u4e14\u6570\u91cf\u4e0d\u591a\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528range\u51fd\u6570\u521b\u5efa\u6574\u578b\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u751f\u6210\u4e00\u7cfb\u5217\u8fde\u7eed\u6574\u6570\u65f6\uff0crange\u51fd\u6570\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u9009\u62e9\u3002range\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u8d77\u59cb\u503c\u3001\u7ec8\u6b62\u503c\u548c\u6b65\u957f\u751f\u6210\u6574\u6570\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528range\u51fd\u6570\u521b\u5efa\u6574\u578b\u5217\u8868<\/p>\n<p>int_list = list(range(1, 11))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0crange\u51fd\u6570\u751f\u6210\u4e86\u4ece1\u523010\u7684\u6574\u6570\u5e8f\u5217\uff0clist\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u751f\u6210\u5927\u8303\u56f4\u8fde\u7eed\u6574\u6570\u5217\u8868\u7684\u60c5\u51b5\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0crange\u51fd\u6570\u4e0d\u5305\u62ec\u7ec8\u6b62\u503c\uff0c\u751f\u6210\u7684\u5e8f\u5217\u662f\u5de6\u95ed\u53f3\u5f00\u7684\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5217\u8868\u89e3\u6790\u521b\u5efa\u6574\u578b\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u5f3a\u5927\u7684Python\u7279\u6027\uff0c\u5141\u8bb8\u4f60\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u751f\u6210\u5217\u8868\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u6574\u6570\u5e8f\u5217\u751f\u6210\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u548c\u8868\u8fbe\u5f0f\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u8868\u89e3\u6790\u521b\u5efa\u6574\u578b\u5217\u8868<\/p>\n<p>int_list = [x for x in range(1, 11)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u751f\u6210\u7684\u6574\u6570\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u8fc7\u6ee4\u7684\u573a\u5408\u3002\u4e2a\u4eba\u5efa\u8bae\u5728\u9700\u8981\u751f\u6210\u590d\u6742\u5217\u8868\u7ed3\u6784\u65f6\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5668\u521b\u5efa\u6574\u578b\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5217\u8868\u751f\u6210\u5668\u662f\u4e00\u79cd\u8282\u7701\u5185\u5b58\u7684\u751f\u6210\u65b9\u5f0f\uff0c\u5c24\u5176\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002\u4e0e\u5217\u8868\u89e3\u6790\u7c7b\u4f3c\uff0c\u5217\u8868\u751f\u6210\u5668\u4f7f\u7528\u5706\u62ec\u53f7\u800c\u4e0d\u662f\u65b9\u62ec\u53f7\uff0c\u5e76\u4e14\u4e0d\u4f1a\u7acb\u5373\u751f\u6210\u6240\u6709\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u751f\u6210\u5668\u521b\u5efa\u6574\u578b\u5217\u8868<\/p>\n<p>int_generator = (x for x in range(1, 11))<\/p>\n<p>int_list = list(int_generator)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u96c6\u6216\u5185\u5b58\u6709\u9650\u7684\u573a\u5408\u3002\u5728\u9700\u8981\u591a\u6b21\u904d\u5386\u751f\u6210\u5668\u65f6\uff0c\u5efa\u8bae\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u4ee5\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u901a\u8fc7numpy\u5e93\u521b\u5efa\u6574\u578b\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u503c\u8ba1\u7b97\u7684\u573a\u5408\uff0cnumpy\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u3002\u4f7f\u7528numpy\u7684arange\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u6574\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528numpy\u521b\u5efa\u6574\u578b\u5217\u8868<\/strong><\/h2>\n<p>int_array = np.arange(1, 11)<\/p>\n<p>int_list = int_array.tolist()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>numpy\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u80fd\u529b\u548c\u4e30\u5bcc\u7684\u51fd\u6570\u5e93<\/strong>\uff0c\u9002\u5408\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u7b49\u9886\u57df\u4e2d\u4f7f\u7528\u3002\u4e2a\u4eba\u5efa\u8bae\u5728\u9700\u8981\u8fdb\u884c\u590d\u6742\u6570\u503c\u8fd0\u7b97\u65f6\u8003\u8651\u4f7f\u7528numpy\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u6574\u578b\u5217\u8868\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5c24\u4e3a\u91cd\u8981\u3002\u76f4\u63a5\u8d4b\u503c\u9002\u5408\u7b80\u5355\u573a\u666f\uff0crange\u51fd\u6570\u548c\u5217\u8868\u89e3\u6790\u5219\u9002\u7528\u4e8e\u751f\u6210\u89c4\u5f8b\u6027\u6574\u6570\u5e8f\u5217\uff0c\u800cnumpy\u5219\u9002\u5408\u9ad8\u6027\u80fd\u6570\u503c\u8ba1\u7b97\u573a\u5408\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u5177\u4f53\u9700\u6c42\u548c\u8d44\u6e90\u9650\u5236\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u521b\u5efa\u65b9\u6cd5\u53ef\u4ee5\u63d0\u5347\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u5efa\u8bae\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20\uff1a\u5217\u8868\u7684\u957f\u5ea6\u548c\u5185\u5bb9\u662f\u5426\u5df2\u77e5\u3001\u5bf9\u751f\u6210\u6548\u7387\u548c\u5185\u5b58\u4f7f\u7528\u7684\u8981\u6c42\u3001\u662f\u5426\u9700\u8981\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u7b49\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u521b\u5efa\u6574\u578b\u5217\u8868\u5e76\u6ee1\u8db3\u5404\u79cd\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7279\u5b9a\u8303\u56f4\u7684\u6574\u578b\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>range()<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u5305\u542b\u7279\u5b9a\u8303\u56f4\u7684\u6574\u578b\u5217\u8868\u3002\u6bd4\u5982\uff0c\u5982\u679c\u60a8\u60f3\u521b\u5efa\u4e00\u4e2a\u4ece1\u523010\u7684\u6574\u578b\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">int_list = list(range(1, 11))\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c06\u5f97\u5230\u4e00\u4e2a\u5305\u542b\u6570\u5b571\u523010\u7684\u5217\u8868\uff1a<code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/code>\u3002<code>range()<\/code>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8d77\u59cb\u503c\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u7ed3\u675f\u503c\uff08\u4e0d\u5305\u62ec\uff09\uff0c\u60a8\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u7b2c\u4e09\u4e2a\u53c2\u6570\u6765\u8bbe\u7f6e\u6b65\u957f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4ece\u7528\u6237\u8f93\u5165\u521b\u5efa\u6574\u578b\u5217\u8868\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u901a\u8fc7<code>input()<\/code>\u51fd\u6570\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u578b\u5217\u8868\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u8f93\u5165\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u4ee5\u9017\u53f7\u5206\u9694\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u7cfb\u5217\u6574\u578b\u6570\u5b57\uff0c\u7528\u9017\u53f7\u5206\u9694\uff1a&quot;)\nint_list = [int(num) for num in user_input.split(&#39;,&#39;)]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5c06\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u5206\u5272\u4e3a\u5355\u4e2a\u6570\u5b57\uff0c\u5e76\u5c06\u6bcf\u4e2a\u6570\u5b57\u8f6c\u6362\u4e3a\u6574\u578b\uff0c\u4ece\u800c\u521b\u5efa\u4e00\u4e2a\u6574\u578b\u5217\u8868\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5408\u5e76\u4e24\u4e2a\u6574\u578b\u5217\u8868\uff1f<\/strong><br \/>\u5408\u5e76\u4e24\u4e2a\u6574\u578b\u5217\u8868\u975e\u5e38\u7b80\u5355\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>+<\/code>\u8fd0\u7b97\u7b26\u6216<code>extend()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u60a8\u6709\u4e24\u4e2a\u5217\u8868<code>list1<\/code>\u548c<code>list2<\/code>\uff0c\u53ef\u4ee5\u8fd9\u6837\u5408\u5e76\u5b83\u4eec\uff1a  <\/p>\n<pre><code class=\"language-python\">list1 = [1, 2, 3]\nlist2 = [4, 5, 6]\nmerged_list = list1 + list2\n<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528<code>extend()<\/code>\u65b9\u6cd5\uff1a  <\/p>\n<pre><code class=\"language-python\">list1.extend(list2)\n<\/code><\/pre>\n<p>\u4e24\u79cd\u65b9\u6cd5\u90fd\u4f1a\u5f97\u5230\u5408\u5e76\u540e\u7684\u5217\u8868<code>[1, 2, 3, 4, 5, 6]<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u521b\u5efa\u6574\u578b\u5217\u8868\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u5229\u7528range\u51fd\u6570\u3001\u4f7f\u7528\u5217\u8868\u751f\u6210\u5668 [&hellip;]","protected":false},"author":3,"featured_media":1022838,"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\/1022834"}],"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=1022834"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1022834\/revisions"}],"predecessor-version":[{"id":1022839,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1022834\/revisions\/1022839"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1022838"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1022834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1022834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1022834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}