{"id":933611,"date":"2024-12-26T18:18:31","date_gmt":"2024-12-26T10:18:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/933611.html"},"modified":"2024-12-26T18:18:33","modified_gmt":"2024-12-26T10:18:33","slug":"python-array%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/933611.html","title":{"rendered":"python array\u5982\u4f55\u5b9a\u4e49"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070952\/47178148-bea7-4ac3-892b-122fb6dd6745.webp\" alt=\"python array\u5982\u4f55\u5b9a\u4e49\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9a\u4e49\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u3001NumPy\u5e93\u7684\u6570\u7ec4\u5bf9\u8c61\u3001array\u6a21\u5757\u7b49\u3002\u5bf9\u4e8e\u7b80\u5355\u5e94\u7528\uff0c\u4f7f\u7528\u5217\u8868\u5373\u53ef\u6ee1\u8db3\u9700\u6c42\uff1b\u800c\u5bf9\u4e8e\u79d1\u5b66\u8ba1\u7b97\u6216\u9700\u8981\u9ad8\u6548\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u7684\u60c5\u51b5\uff0cNumPy\u5e93\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u5b9a\u4e49\u6570\u7ec4\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u5206\u6790\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u5b9a\u4e49\u6570\u7ec4<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5217\u8868\u662f\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u4eec\u7075\u6d3b\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u5217\u8868\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u4f46\u5728\u5b9a\u4e49\u6570\u7ec4\u65f6\uff0c\u901a\u5e38\u5b58\u50a8\u540c\u4e00\u7c7b\u578b\u7684\u5143\u7d20\u4ee5\u4fbf\u4e8e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u7b80\u5355\u7684\u5217\u8868<\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u5217\u8868\u53ef\u4ee5\u901a\u8fc7\u65b9\u62ec\u53f7<code>[]<\/code>\u6765\u5b9e\u73b0\u3002\u5217\u8868\u4e2d\u7684\u5143\u7d20\u7528\u9017\u53f7\u5206\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5217\u8868\u7684\u4f18\u7f3a\u70b9<\/li>\n<\/ol>\n<ul>\n<li>\u4f18\u70b9\uff1a\u7b80\u5355\u6613\u7528\uff0c\u652f\u6301\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5185\u5efa\u65b9\u6cd5\uff08\u5982<code>append()<\/code>\u3001<code>extend()<\/code>\u3001<code>insert()<\/code>\u7b49\uff09\u6765\u64cd\u4f5c\u6570\u636e\u3002<\/li>\n<li>\u7f3a\u70b9\uff1a\u5f53\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u6216\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u65f6\uff0c\u6548\u7387\u8f83\u4f4e\u3002<\/li>\n<\/ul>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u5b9a\u4e49\u6570\u7ec4<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5f00\u6e90Python\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u591a\u79cd\u6570\u5b66\u51fd\u6570\u3002NumPy\u6570\u7ec4\u5177\u6709\u56fa\u5b9a\u5927\u5c0f\u548c\u9ad8\u6548\u7684\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5NumPy\u5e93<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efaNumPy\u6570\u7ec4<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528NumPy\u521b\u5efa\u6570\u7ec4\u65f6\uff0c\u901a\u5e38\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 2, 3, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>NumPy\u6570\u7ec4\u7684\u4f18\u7f3a\u70b9<\/li>\n<\/ol>\n<ul>\n<li>\u4f18\u70b9\uff1a\u9ad8\u6548\u7684\u5185\u5b58\u4f7f\u7528\u548c\u8ba1\u7b97\u80fd\u529b\uff0c\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u548c\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\uff0c\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u548c\u590d\u6742\u7684\u79d1\u5b66\u8ba1\u7b97\u3002<\/li>\n<li>\u7f3a\u70b9\uff1a\u9700\u8981\u989d\u5916\u5b89\u88c5NumPy\u5e93\uff0c\u5b66\u4e60\u66f2\u7ebf\u76f8\u5bf9\u8f83\u9661\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u4f7f\u7528array\u6a21\u5757\u5b9a\u4e49\u6570\u7ec4<\/p>\n<\/p>\n<p><p><code>array<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u57fa\u672c\u7684\u6570\u7ec4\u652f\u6301\uff0c\u5b83\u6bd4\u5217\u8868\u66f4\u9ad8\u6548\uff0c\u4f46\u4e0d\u5982NumPy\u529f\u80fd\u5f3a\u5927\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165\u548c\u521b\u5efa\u6570\u7ec4<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import array<\/p>\n<p>array = array.array(&#39;i&#39;, [1, 2, 3, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>&#39;i&#39;<\/code>\u8868\u793a\u6570\u7ec4\u5b58\u50a8\u7684\u662f\u6574\u6570\u7c7b\u578b\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>array\u6a21\u5757\u7684\u4f18\u7f3a\u70b9<\/li>\n<\/ol>\n<ul>\n<li>\u4f18\u70b9\uff1a\u76f8\u6bd4\u5217\u8868\uff0c<code>array<\/code>\u6a21\u5757\u5728\u5b58\u50a8\u540c\u7c7b\u578b\u5143\u7d20\u65f6\u66f4\u9ad8\u6548\u3002<\/li>\n<li>\u7f3a\u70b9\uff1a\u529f\u80fd\u6709\u9650\uff0c\u4e0d\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u548c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/li>\n<\/ul>\n<p><p>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u6570\u7ec4\u5b9a\u4e49\u65b9\u5f0f<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5982\u4f55\u5b9a\u4e49\u6570\u7ec4\u65f6\uff0c\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u51e0\u4e2a\u56e0\u7d20\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u89c4\u6a21\u548c\u590d\u6742\u6027<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u5c0f\u578b\u6570\u636e\u96c6\uff0c\u4f7f\u7528\u5217\u8868\u5373\u53ef\uff1b\u800c\u5bf9\u4e8e\u9700\u8981\u9ad8\u6027\u80fd\u548c\u5904\u7406\u591a\u7ef4\u6570\u636e\u7684\u573a\u666f\uff0cNumPy\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u7b97\u9700\u6c42<\/strong>\uff1a\u5982\u679c\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u6216\u7ebf\u6027\u4ee3\u6570\u8ba1\u7b97\uff0cNumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u5e93\uff0c\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5185\u5b58\u4f7f\u7528<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u7684\u5e94\u7528\uff0c\u4f7f\u7528NumPy\u6216<code>array<\/code>\u6a21\u5757\u4f1a\u6bd4\u5217\u8868\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u4fdd\u6301\u4ee3\u7801\u7b80\u5355\u548c\u53ef\u8bfb\u6027\u9ad8\u662f\u9996\u8981\u4efb\u52a1\uff0c\u56e0\u6b64\u5217\u8868\u53ef\u80fd\u662f\u6700\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5bf9\u4ee5\u4e0a\u51e0\u79cd\u6570\u7ec4\u5b9a\u4e49\u65b9\u5f0f\u7684\u7406\u89e3\u548c\u6bd4\u8f83\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u5b9e\u73b0Python\u7a0b\u5e8f\u4e2d\u6570\u7ec4\u7684\u9ad8\u6548\u7ba1\u7406\u548c\u64cd\u4f5c\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\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6570\u7ec4\u901a\u5e38\u4f7f\u7528<code>list<\/code>\u6216\u8005<code>array<\/code>\u6a21\u5757\u6765\u521b\u5efa\u3002\u5982\u679c\u4f7f\u7528<code>list<\/code>\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u65b9\u62ec\u53f7\u5b9a\u4e49\uff0c\u5982<code>my_list = [1, 2, 3, 4]<\/code>\u3002\u5982\u679c\u9700\u8981\u4f7f\u7528<code>array<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import array<\/code>\u6765\u5bfc\u5165\uff0c\u7136\u540e\u4f7f\u7528<code>array<\/code>\u51fd\u6570\u6765\u5b9a\u4e49\u6570\u7ec4\uff0c\u4f8b\u5982\uff1a<code>import array as arr<\/code>\uff0c\u63a5\u7740\u53ef\u4ee5\u4f7f\u7528<code>my_array = arr.array(&#39;i&#39;, [1, 2, 3, 4])<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u6574\u6570\u7c7b\u578b\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>Python\u6570\u7ec4\u4e0e\u5217\u8868\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>Python\u7684\u6570\u7ec4\u548c\u5217\u8868\u6709\u7740\u663e\u8457\u7684\u533a\u522b\u3002\u6570\u7ec4\u662f\u56fa\u5b9a\u7c7b\u578b\u7684\uff0c\u53ef\u4ee5\u5b58\u50a8\u76f8\u540c\u6570\u636e\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u901a\u5e38\u5728\u9700\u8981\u9ad8\u6548\u5b58\u50a8\u548c\u8ba1\u7b97\u7684\u573a\u666f\u4e2d\u4f7f\u7528\u3002\u800c\u5217\u8868\u662f\u52a8\u6001\u7c7b\u578b\u7684\uff0c\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\u548c\u4fbf\u5229\u6027\u3002\u5728\u6027\u80fd\u65b9\u9762\uff0c\u6570\u7ec4\u5728\u5904\u7406\u5927\u91cf\u6570\u503c\u8fd0\u7b97\u65f6\u66f4\u4f18\uff0c\u800c\u5217\u8868\u5219\u9002\u7528\u4e8e\u4e00\u822c\u7684\u6570\u636e\u5b58\u50a8\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u7ec4\u5143\u7d20\uff1f<\/strong><br \/>\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u7ec4\u5143\u7d20\u975e\u5e38\u7b80\u5355\u3002\u5bf9\u4e8e\u5217\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\uff0c\u4f8b\u5982<code>my_list[0]<\/code>\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5982\u679c\u8981\u4fee\u6539\u8be5\u5143\u7d20\uff0c\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\uff0c\u5982<code>my_list[0] = 10<\/code>\u3002\u5bf9\u4e8e\u4f7f\u7528<code>array<\/code>\u6a21\u5757\u521b\u5efa\u7684\u6570\u7ec4\uff0c\u8bbf\u95ee\u548c\u4fee\u6539\u7684\u65b9\u6cd5\u76f8\u540c\uff0c\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\uff0c\u4f8b\u5982<code>my_array[0]<\/code>\uff0c\u5e76\u901a\u8fc7\u8d4b\u503c\u6765\u4fee\u6539\u5143\u7d20\u7684\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9a\u4e49\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u3001NumPy\u5e93\u7684\u6570\u7ec4\u5bf9\u8c61\u3001array\u6a21\u5757\u7b49\u3002\u5bf9\u4e8e\u7b80\u5355 [&hellip;]","protected":false},"author":3,"featured_media":933615,"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\/933611"}],"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=933611"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933611\/revisions"}],"predecessor-version":[{"id":933618,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933611\/revisions\/933618"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/933615"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=933611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=933611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=933611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}