{"id":1057386,"date":"2024-12-31T15:07:41","date_gmt":"2024-12-31T07:07:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1057386.html"},"modified":"2024-12-31T15:07:43","modified_gmt":"2024-12-31T07:07:43","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e4%b8%89%e7%bb%b4%e7%9a%84%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1057386.html","title":{"rendered":"python\u5982\u4f55\u751f\u6210\u4e09\u7ef4\u7684\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/d9caaba2-b021-4877-8293-d9495d05b9d4.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u751f\u6210\u4e09\u7ef4\u7684\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210\u4e09\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><strong>Python\u751f\u6210\u4e09\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u64cd\u4f5c\u6570\u7ec4\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u7684\u65b9\u5f0f\u624b\u52a8\u521b\u5efa\u4e09\u7ef4\u6570\u7ec4\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982NumPy\u9ad8\u6548\uff0c\u4f46\u5728\u4e00\u4e9b\u7b80\u5355\u7684\u60c5\u51b5\u4e0b\u4ecd\u7136\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a 3x3x3 \u7684\u4e09\u7ef4\u6570\u7ec4<\/p>\n<p>three_dim_array = [[[0 for _ in range(3)] for _ in range(3)] for _ in range(3)]<\/p>\n<p>print(three_dim_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u4e00\u4e2a3x3x3\u7684\u4e09\u7ef4\u6570\u7ec4\uff0c\u6240\u6709\u5143\u7d20\u90fd\u521d\u59cb\u5316\u4e3a0\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u66f4\u6539\u6570\u7ec4\u7684\u5927\u5c0f\u548c\u521d\u59cb\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u662f\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u7684\u6700\u4f73\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86\u521b\u5efa\u548c\u64cd\u4f5c\u591a\u7ef4\u6570\u7ec4\u7684\u9ad8\u6548\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528NumPy\u521b\u5efa\u4e09\u7ef4\u6570\u7ec4\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a 3x3x3 \u7684\u4e09\u7ef4\u6570\u7ec4\uff0c\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a0<\/strong><\/h2>\n<p>three_dim_array = np.zeros((3, 3, 3))<\/p>\n<p>print(three_dim_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>NumPy\u521b\u5efa\u4e09\u7ef4\u6570\u7ec4\u7684\u4f18\u52bf\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u9ad8\u6548\u6027<\/strong>\uff1aNumPy\u7684\u5e95\u5c42\u5b9e\u73b0\u4f7f\u7528C\u8bed\u8a00\uff0c\u901f\u5ea6\u5feb\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5c\u6570\u7ec4\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027<\/strong>\uff1a\u4e0eSciPy\u3001Pandas\u7b49\u5176\u4ed6\u79d1\u5b66\u8ba1\u7b97\u5e93\u517c\u5bb9\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\u3002\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u521b\u5efa\u4e09\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a 3x3x3 \u7684\u4e09\u7ef4\u6570\u7ec4\uff0c\u6240\u6709\u5143\u7d20\u521d\u59cb\u5316\u4e3a0<\/p>\n<p>three_dim_array = [[[0 for _ in range(3)] for _ in range(3)] for _ in range(3)]<\/p>\n<p>print(three_dim_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7c7b\u4f3c\u4e8e\u4f7f\u7528\u5d4c\u5957\u5217\u8868\uff0c\u4f46\u66f4\u52a0\u7b80\u6d01\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u66f4\u6539\u6570\u7ec4\u7684\u5927\u5c0f\u548c\u521d\u59cb\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e09\u7ef4\u6570\u7ec4\u5e38\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u3001\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7b49\u9886\u57df\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0c\u56fe\u50cf\u901a\u5e38\u8868\u793a\u4e3a\u4e09\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u7b2c\u4e00\u7ef4\u8868\u793a\u9ad8\u5ea6\uff0c\u7b2c\u4e8c\u7ef4\u8868\u793a\u5bbd\u5ea6\uff0c\u7b2c\u4e09\u7ef4\u8868\u793a\u989c\u8272\u901a\u9053\uff08\u5982RGB\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>\u6253\u5f00\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u4e09\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>image_array = np.array(image)<\/p>\n<p>print(image_array.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528PIL\u5e93\u6253\u5f00\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u4e09\u7ef4\u6570\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u5bf9\u56fe\u50cf\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u88c1\u526a\u3001\u65cb\u8f6c\u3001\u6ee4\u6ce2\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u79d1\u5b66\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u4e09\u7ef4\u6570\u7ec4\u5e38\u7528\u4e8e\u8868\u793a\u4e09\u7ef4\u7a7a\u95f4\u4e2d\u7684\u6570\u636e\uff0c\u5982\u6e29\u5ea6\u3001\u538b\u529b\u3001\u901f\u5ea6\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a 10x10x10 \u7684\u4e09\u7ef4\u6570\u7ec4\uff0c\u6240\u6709\u5143\u7d20\u968f\u673a\u521d\u59cb\u5316<\/strong><\/h2>\n<p>three_dim_array = np.random.rand(10, 10, 10)<\/p>\n<p>print(three_dim_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528NumPy\u7684<code>rand<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a10x10x10\u7684\u4e09\u7ef4\u6570\u7ec4\uff0c\u6240\u6709\u5143\u7d20\u968f\u673a\u521d\u59cb\u5316\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u5bf9\u6570\u636e\u8fdb\u884c\u5404\u79cd\u8ba1\u7b97\u548c\u64cd\u4f5c\uff0c\u5982\u6c42\u548c\u3001\u5747\u503c\u3001\u6807\u51c6\u5dee\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u4e09\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u3002<strong>\u5176\u4e2dNumPy\u5e93\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e09\u7ef4\u6570\u7ec4\u5e38\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u3001\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7b49\u9886\u57df\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u548c\u64cd\u4f5c\u4e09\u7ef4\u6570\u636e\uff0c\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u548c\u89e3\u51b3\u95ee\u9898\u7684\u80fd\u529b\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\u4e09\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u4e09\u7ef4\u6570\u7ec4\u3002\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install numpy<\/code>\u6765\u5b89\u88c5\u3002\u521b\u5efa\u4e09\u7ef4\u6570\u7ec4\u7684\u57fa\u672c\u65b9\u5f0f\u662f\u4f7f\u7528<code>numpy.array()<\/code>\u6216<code>numpy.zeros()<\/code>\u7b49\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy.zeros((\u6df1\u5ea6, \u884c, \u5217))<\/code>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5168\u96f6\u7684\u4e09\u7ef4\u6570\u7ec4\u3002<\/p>\n<p><strong>Python\u751f\u6210\u4e09\u7ef4\u6570\u7ec4\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4e09\u7ef4\u6570\u7ec4\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u56fe\u50cf\u5904\u7406\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u9886\u57df\u975e\u5e38\u5e38\u89c1\u3002\u6bd4\u5982\uff0c\u5728\u56fe\u50cf\u5904\u7406\u65f6\uff0c\u6bcf\u4e00\u5e45\u56fe\u50cf\u53ef\u4ee5\u8868\u793a\u4e3a\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u989c\u8272\u901a\u9053\uff08\u5982RGB\uff09\u7684\u4fe1\u606f\u3002\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u4e09\u7ef4\u6570\u7ec4\u5e38\u7528\u4e8e\u5b58\u50a8\u591a\u7ef4\u6570\u636e\u96c6\uff0c\u5982\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u6216\u89c6\u9891\u5e27\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u548c\u4fee\u6539Python\u4e2d\u7684\u4e09\u7ef4\u6570\u7ec4\u5143\u7d20\uff1f<\/strong><br \/>\u8bbf\u95ee\u548c\u4fee\u6539\u4e09\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u591a\u7ef4\u7d22\u5f15\u5b8c\u6210\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>arr<\/code>\u7684\u4e09\u7ef4\u6570\u7ec4\uff0c<code>arr[\u6df1\u5ea6][\u884c][\u5217]<\/code>\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u6216\u4fee\u6539\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5207\u7247\u6765\u83b7\u53d6\u66f4\u5927\u8303\u56f4\u7684\u5b50\u6570\u7ec4\uff0c\u8fd9\u5bf9\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u975e\u5e38\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210\u4e09\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5 Python\u751f\u6210\u4e09\u7ef4\u6570\u7ec4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1057405,"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\/1057386"}],"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=1057386"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057386\/revisions"}],"predecessor-version":[{"id":1057408,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1057386\/revisions\/1057408"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1057405"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1057386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1057386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1057386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}