{"id":1132227,"date":"2025-01-08T20:55:16","date_gmt":"2025-01-08T12:55:16","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132227.html"},"modified":"2025-01-08T20:55:18","modified_gmt":"2025-01-08T12:55:18","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e4%b8%80%e4%b8%aa%e6%95%b0%e7%bb%84%e7%9a%84%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132227.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101836\/4a3e13b2-cff3-4675-9376-af314c2e7db1.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9a\u4e49\u6570\u7ec4\u7684\u957f\u5ea6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5217\u8868\u3001NumPy\u5e93\u3001array\u6a21\u5757\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7Python\u7684\u5185\u7f6e\u5217\u8868\u548cNumPy\u5e93\u3002<\/strong> \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Python\u5185\u7f6e\u5217\u8868<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u5217\u8868\uff08List\uff09\u662f\u5b9a\u4e49\u548c\u64cd\u4f5c\u6570\u7ec4\u7684\u6700\u5e38\u7528\u5de5\u5177\u3002\u5217\u8868\u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u6539\u53d8\u5176\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h4>1. \u521d\u59cb\u5316\u5217\u8868\u5e76\u5b9a\u4e49\u957f\u5ea6<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\u5e76\u8bbe\u7f6e\u5176\u521d\u59cb\u957f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5305\u542b5\u4e2a\u5143\u7d20\u7684\u5217\u8868\uff0c\u521d\u59cb\u503c\u4e3a0<\/p>\n<p>array = [0] * 5<\/p>\n<p>print(array)  # \u8f93\u51fa: [0, 0, 0, 0, 0]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b3\u4e2a\u5143\u7d20\u7684\u5217\u8868\uff0c\u521d\u59cb\u503c\u4e3aNone<\/strong><\/h2>\n<p>array = [None] * 3<\/p>\n<p>print(array)  # \u8f93\u51fa: [None, None, None]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a8\u6001\u8c03\u6574\u5217\u8868\u957f\u5ea6<\/h4>\n<\/p>\n<p><p>\u7531\u4e8ePython\u5217\u8868\u662f\u52a8\u6001\u7684\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u968f\u65f6\u8c03\u6574\u5176\u957f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [1, 2, 3]<\/p>\n<p>array.append(4)  # \u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>print(array)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p>array.pop()  # \u79fb\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>print(array)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5904\u7406\u529f\u80fd\u3002NumPy\u6570\u7ec4\uff08ndarray\uff09\u662f\u4e00\u4e2a\u56fa\u5b9a\u5927\u5c0f\u7684\u591a\u7ef4\u6570\u7ec4\uff0c\u6bd4Python\u5185\u7f6e\u5217\u8868\u5177\u6709\u66f4\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5NumPy<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521d\u59cb\u5316NumPy\u6570\u7ec4\u5e76\u5b9a\u4e49\u957f\u5ea6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u521d\u59cb\u5316\u4e00\u4e2aNumPy\u6570\u7ec4\u5e76\u8bbe\u7f6e\u5176\u957f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b5\u4e2a\u5143\u7d20\u7684NumPy\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a0<\/strong><\/h2>\n<p>array = np.zeros(5)<\/p>\n<p>print(array)  # \u8f93\u51fa: [0. 0. 0. 0. 0.]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b3\u4e2a\u5143\u7d20\u7684NumPy\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a1<\/strong><\/h2>\n<p>array = np.ones(3)<\/p>\n<p>print(array)  # \u8f93\u51fa: [1. 1. 1.]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u52a8\u6001\u8c03\u6574NumPy\u6570\u7ec4\u957f\u5ea6<\/h4>\n<\/p>\n<p><p>\u5c3d\u7ba1NumPy\u6570\u7ec4\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u5206\u914d\u6570\u7ec4\u6765\u6539\u53d8\u5176\u957f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 2, 3])<\/p>\n<p>array = np.append(array, 4)  # \u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>print(array)  # \u8f93\u51fa: [1 2 3 4]<\/p>\n<p>array = np.delete(array, -1)  # \u79fb\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>print(array)  # \u8f93\u51fa: [1 2 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528array\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684array\u6a21\u5757\u63d0\u4f9b\u4e86\u6570\u7ec4\u652f\u6301\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u5b58\u50a8\u5927\u91cf\u6570\u503c\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165array\u6a21\u5757\u5e76\u521d\u59cb\u5316\u6570\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import array<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b5\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a0<\/strong><\/h2>\n<p>array = array.array(&#39;i&#39;, [0] * 5)<\/p>\n<p>print(array)  # \u8f93\u51fa: array(&#39;i&#39;, [0, 0, 0, 0, 0])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b3\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a1<\/strong><\/h2>\n<p>array = array.array(&#39;i&#39;, [1] * 3)<\/p>\n<p>print(array)  # \u8f93\u51fa: array(&#39;i&#39;, [1, 1, 1])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a8\u6001\u8c03\u6574\u6570\u7ec4\u957f\u5ea6<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7array\u6a21\u5757\uff0c\u53ef\u4ee5\u52a8\u6001\u8c03\u6574\u6570\u7ec4\u7684\u957f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import array<\/p>\n<p>array = array.array(&#39;i&#39;, [1, 2, 3])<\/p>\n<p>array.append(4)  # \u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>print(array)  # \u8f93\u51fa: array(&#39;i&#39;, [1, 2, 3, 4])<\/p>\n<p>array.pop()  # \u79fb\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>print(array)  # \u8f93\u51fa: array(&#39;i&#39;, [1, 2, 3])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u4f55\u79cd\u65b9\u6cd5\u5b9a\u4e49\u6570\u7ec4\u957f\u5ea6\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>Python\u5185\u7f6e\u5217\u8868<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u591a\u6570\u901a\u7528\u573a\u666f\uff0c\u7075\u6d3b\u4e14\u6613\u7528\u3002<\/li>\n<li><strong>NumPy\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6027\u80fd\u548c\u9ad8\u7ea7\u6570\u7ec4\u64cd\u4f5c\u7684\u79d1\u5b66\u8ba1\u7b97\u573a\u666f\u3002<\/li>\n<li><strong>array\u6a21\u5757<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u5b58\u50a8\u548c\u64cd\u4f5c\u5927\u91cf\u6570\u503c\u6570\u636e\u7684\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9a\u4e49\u548c\u64cd\u4f5c\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u5e0c\u671b\u672c\u6587\u80fd\u4e3a\u60a8\u5728Python\u4e2d\u5904\u7406\u6570\u7ec4\u63d0\u4f9b\u6709\u7528\u7684\u53c2\u8003\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\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528\u5217\u8868\u6765\u5b9e\u73b0\u6570\u7ec4\u7684\u529f\u80fd\uff0c\u4f46\u5217\u8868\u7684\u957f\u5ea6\u662f\u52a8\u6001\u7684\u3002\u5982\u679c\u60f3\u521b\u5efa\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>array<\/code>\u6a21\u5757\u6216<code>numpy<\/code>\u5e93\u3002\u4f7f\u7528<code>numpy<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>numpy.zeros()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a0\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\narray = np.zeros(5)  # \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u6570\u7ec4\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u68c0\u67e5\u6570\u7ec4\u7684\u957f\u5ea6\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>len()<\/code>\u51fd\u6570\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5217\u8868\u8fd8\u662f<code>numpy<\/code>\u6570\u7ec4\uff0c\u8c03\u7528<code>len(array)<\/code>\u90fd\u4f1a\u8fd4\u56de\u6570\u7ec4\u7684\u5143\u7d20\u4e2a\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3]\nlength = len(my_list)  # \u8fd4\u56de3\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u65b9\u5f0f\u6765\u5b9a\u4e49\u6570\u7ec4\u7684\u5927\u5c0f\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>numpy<\/code>\uff0cPython\u7684<code>array<\/code>\u6a21\u5757\u4e5f\u5141\u8bb8\u7528\u6237\u521b\u5efa\u6570\u7ec4\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u7c7b\u578b\u548c\u521d\u59cb\u5927\u5c0f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import array as arr\nmy_array = arr.array(&#39;i&#39;, [0]*5)  # \u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a5\u7684\u6574\u578b\u6570\u7ec4\uff0c\u521d\u59cb\u503c\u4e3a0\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u548c\u5927\u5c0f\u7684\u6570\u7ec4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9a\u4e49\u6570\u7ec4\u7684\u957f\u5ea6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5217\u8868\u3001NumPy\u5e93\u3001array\u6a21\u5757\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7Pyt [&hellip;]","protected":false},"author":3,"featured_media":1132233,"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\/1132227"}],"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=1132227"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132227\/revisions"}],"predecessor-version":[{"id":1132235,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132227\/revisions\/1132235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132233"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}