{"id":1059980,"date":"2024-12-31T15:29:57","date_gmt":"2024-12-31T07:29:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1059980.html"},"modified":"2024-12-31T15:29:59","modified_gmt":"2024-12-31T07:29:59","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e4%b8%80%e4%b8%aa%e7%a9%baarray","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1059980.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u7a7aarray"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/7abb5734-8ca5-44c7-afed-19548706bc84.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u7a7aarray\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u7a7aarray<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u6570\u7ec4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u3001NumPy\u5e93\u548carray\u6a21\u5757\u7b49\u3002<strong>\u4f7f\u7528\u7a7a\u5217\u8868\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528array\u6a21\u5757<\/strong>\u662f\u4e09\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u5e76\u7ed9\u51fa\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7a7a\u5217\u8868<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684\u5217\u8868\u7c7b\u578b\u662f\u6700\u5e38\u89c1\u548c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u5217\u8868\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f7f\u7528\u4e00\u5bf9\u65b9\u62ec\u53f7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">empty_list = []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a\u4f7f\u7528\u7a7a\u5217\u8868\u5b9a\u4e49\u6570\u7ec4\u975e\u5e38\u7b80\u5355\uff0c\u9002\u5408\u5904\u7406\u4e00\u822c\u7684\u6570\u7ec4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u5f53\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u6570\u503c\u8ba1\u7b97\u6216\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982\u4e13\u95e8\u7684\u5e93\uff08\u5982NumPy\uff09\u9ad8\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8fdb\u884c\u589e\u5220\u6539\u67e5\u7b49\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u5143\u7d20<\/p>\n<p>empty_list.append(1)<\/p>\n<p>empty_list.append(2)<\/p>\n<h2><strong>\u8bbf\u95ee\u5143\u7d20<\/strong><\/h2>\n<p>print(empty_list[0])  # \u8f93\u51fa\uff1a1<\/p>\n<h2><strong>\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>del empty_list[1]<\/p>\n<p>print(empty_list)  # \u8f93\u51fa\uff1a[1]<\/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\u975e\u5e38\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u4f7f\u7528NumPy\u5b9a\u4e49\u7a7a\u6570\u7ec4\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4f7f\u7528<code>numpy.array<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\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><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>empty_array = np.array([])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1aNumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u9002\u5408\u8fdb\u884c\u9ad8\u6027\u80fd\u7684\u6570\u503c\u8ba1\u7b97\u548c\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u989d\u5916\u5b89\u88c5NumPy\u5e93\uff0c\u4f7f\u7528\u4e0a\u6bd4\u5217\u8868\u7a0d\u5fae\u590d\u6742\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u6570\u7ec4<\/strong><\/h2>\n<p>empty_array = np.array([])<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20\uff08\u901a\u8fc7\u521b\u5efa\u65b0\u7684\u6570\u7ec4\uff09<\/strong><\/h2>\n<p>empty_array = np.append(empty_array, [1, 2, 3])<\/p>\n<h2><strong>\u8bbf\u95ee\u5143\u7d20<\/strong><\/h2>\n<p>print(empty_array[0])  # \u8f93\u51fa\uff1a1<\/p>\n<h2><strong>\u5220\u9664\u5143\u7d20\uff08\u901a\u8fc7\u521b\u5efa\u65b0\u7684\u6570\u7ec4\uff09<\/strong><\/h2>\n<p>empty_array = np.delete(empty_array, 1)<\/p>\n<p>print(empty_array)  # \u8f93\u51fa\uff1a[1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528array\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u4e2d\u7684<code>array<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u6570\u7ec4\u3002\u4e0e\u5217\u8868\u4e0d\u540c\u7684\u662f\uff0c<code>array<\/code>\u6a21\u5757\u521b\u5efa\u7684\u6570\u7ec4\u53ea\u80fd\u5305\u542b\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5bfc\u5165<code>array<\/code>\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import array<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">empty_array = array.array(&#39;i&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>&#39;i&#39;<\/code>\u8868\u793a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u7c7b\u578b\u4e3a\u6574\u6570\u3002\u5176\u4ed6\u5e38\u89c1\u7684\u7c7b\u578b\u4ee3\u7801\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>&#39;b&#39;<\/code>\uff1a\u6709\u7b26\u53f7\u5b57\u7b26<\/li>\n<li><code>&#39;B&#39;<\/code>\uff1a\u65e0\u7b26\u53f7\u5b57\u7b26<\/li>\n<li><code>&#39;u&#39;<\/code>\uff1aUnicode\u5b57\u7b26<\/li>\n<li><code>&#39;h&#39;<\/code>\uff1a\u6709\u7b26\u53f7\u77ed\u6574\u578b<\/li>\n<li><code>&#39;H&#39;<\/code>\uff1a\u65e0\u7b26\u53f7\u77ed\u6574\u578b<\/li>\n<li><code>&#39;i&#39;<\/code>\uff1a\u6709\u7b26\u53f7\u6574\u578b<\/li>\n<li><code>&#39;I&#39;<\/code>\uff1a\u65e0\u7b26\u53f7\u6574\u578b<\/li>\n<li><code>&#39;l&#39;<\/code>\uff1a\u6709\u7b26\u53f7\u957f\u6574\u578b<\/li>\n<li><code>&#39;L&#39;<\/code>\uff1a\u65e0\u7b26\u53f7\u957f\u6574\u578b<\/li>\n<li><code>&#39;f&#39;<\/code>\uff1a\u6d6e\u70b9\u578b<\/li>\n<li><code>&#39;d&#39;<\/code>\uff1a\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u578b<\/li>\n<\/ul>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<code>array<\/code>\u6a21\u5757\u521b\u5efa\u7684\u6570\u7ec4\u6bd4\u5217\u8868\u66f4\u7d27\u51d1\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u529f\u80fd\u4e0d\u5982NumPy\u4e30\u5bcc\uff0c\u64cd\u4f5c\u4e0a\u4e5f\u4e0d\u5982\u5217\u8868\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import array<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u6570\u7ec4<\/strong><\/h2>\n<p>empty_array = array.array(&#39;i&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>empty_array.append(1)<\/p>\n<p>empty_array.append(2)<\/p>\n<h2><strong>\u8bbf\u95ee\u5143\u7d20<\/strong><\/h2>\n<p>print(empty_array[0])  # \u8f93\u51fa\uff1a1<\/p>\n<h2><strong>\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>del empty_array[1]<\/p>\n<p>print(empty_array)  # \u8f93\u51fa\uff1aarray(&#39;i&#39;, [1])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u6570\u7ec4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u3001NumPy\u5e93\u548carray\u6a21\u5757\u7b49\u3002<strong>\u4f7f\u7528\u7a7a\u5217\u8868\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528array\u6a21\u5757<\/strong>\u662f\u4e09\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u7a7a\u5217\u8868<\/strong>\uff1a\u9002\u5408\u5904\u7406\u4e00\u822c\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u7b80\u5355\u6613\u7528\u3002<\/li>\n<li><strong>\u4f7f\u7528NumPy\u5e93<\/strong>\uff1a\u9002\u5408\u8fdb\u884c\u9ad8\u6027\u80fd\u7684\u6570\u503c\u8ba1\u7b97\u548c\u5904\u7406\u591a\u7ef4\u6570\u7ec4\uff0c\u529f\u80fd\u5f3a\u5927\u3002<\/li>\n<li><strong>\u4f7f\u7528array\u6a21\u5757<\/strong>\uff1a\u9002\u5408\u5904\u7406\u5927\u91cf\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u6bd4\u5217\u8868\u66f4\u7d27\u51d1\u3002<\/li>\n<\/ul>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3Python\u4e2d\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u5e76\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u6cd5\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\u7a7a\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u6570\u7ec4\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86<code>numpy<\/code>\u5e93\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684<code>numpy<\/code>\u6570\u7ec4\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nempty_array = np.array([])\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0cPython\u7684\u5185\u7f6e\u5217\u8868\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u6570\u7ec4\u7684\u66ff\u4ee3\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">empty_list = []\n<\/code><\/pre>\n<p><strong>\u7a7a\u6570\u7ec4\u7684\u7528\u9014\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7a7a\u6570\u7ec4\u5728\u6570\u636e\u5904\u7406\u548c\u7f16\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5148\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u6570\u7ec4\u4ee5\u4fbf\u540e\u7eed\u586b\u5145\u6570\u636e\u3002\u5b83\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u67d0\u4e9b\u7b97\u6cd5\u7684\u521d\u59cb\u5316\u6b65\u9aa4\uff0c\u4ee5\u4fbf\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u9010\u6b65\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5411\u7a7a\u6570\u7ec4\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1f<\/strong><br \/>\u5728<code>numpy<\/code>\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.append()<\/code>\u51fd\u6570\u6765\u5411\u7a7a\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">empty_array = np.append(empty_array, new_value)\n<\/code><\/pre>\n<p>\u5982\u679c\u4f7f\u7528Python\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\uff1a  <\/p>\n<pre><code class=\"language-python\">empty_list.append(new_value)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4f60\u5728\u52a8\u6001\u751f\u6210\u6570\u636e\u65f6\u7075\u6d3b\u5730\u6269\u5c55\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u7a7aarray \u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u6570\u7ec4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u3001NumPy\u5e93\u548c [&hellip;]","protected":false},"author":3,"featured_media":1059994,"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\/1059980"}],"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=1059980"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059980\/revisions"}],"predecessor-version":[{"id":1059995,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059980\/revisions\/1059995"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1059994"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1059980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1059980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1059980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}