{"id":1123930,"date":"2025-01-08T19:36:59","date_gmt":"2025-01-08T11:36:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1123930.html"},"modified":"2025-01-08T19:37:02","modified_gmt":"2025-01-08T11:37:02","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python3%e5%b0%86%e5%88%97%e8%a1%a8%e7%94%9f%e6%88%90%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1123930.html","title":{"rendered":"\u5982\u4f55\u5728python3\u5c06\u5217\u8868\u751f\u6210\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085214\/f4bc494f-79da-4490-95b7-ca722553f029.webp\" alt=\"\u5982\u4f55\u5728python3\u5c06\u5217\u8868\u751f\u6210\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python3\u4e2d\u5c06\u5217\u8868\u751f\u6210\u6570\u7ec4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u7684<code>array()<\/code>\u51fd\u6570\u3001Python\u5185\u7f6e\u7684array\u6a21\u5757\u3001\u6216\u8005\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528numpy\u5e93\u7684<code>array()<\/code>\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u4f18\u5316\u3002\u4e0b\u9762\u5c31\u8be6\u7ec6\u8bb2\u89e3\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u3001\u5404\u79cd\u6d3e\u751f\u5bf9\u8c61\uff08\u5982masked arrays\u548c\u77e9\u9635\uff09\u4ee5\u53ca\u7528\u4e8e\u5feb\u901f\u64cd\u4f5c\u6570\u7ec4\u7684\u51fd\u6570\u3002NumPy\u7684\u4e3b\u8981\u5bf9\u8c61\u662f\u540c\u8d28\u7684\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5NumPy<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u8fd9\u4e2a\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5c06\u5217\u8868\u8f6c\u4e3a\u6570\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528numpy\u7684array\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/h2>\n<p>my_array = np.array(my_list)<\/p>\n<p>print(my_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>[1 2 3 4 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>NumPy\u6570\u7ec4\u7684\u4f18\u52bf<\/h4>\n<\/p>\n<ol>\n<li><strong>\u9ad8\u6548\u7684\u6570\u503c\u8fd0\u7b97<\/strong>\uff1aNumPy\u6570\u7ec4\u5728\u6570\u503c\u8ba1\u7b97\u65b9\u9762\u6bd4Python\u5217\u8868\u66f4\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5927\u89c4\u6a21\u6570\u636e\u7684\u5904\u7406\u3002<\/li>\n<li><strong>\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570<\/strong>\uff1aNumPy\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u6570\u7ec4\u4e0a\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u591a\u7ef4\u6570\u7ec4\u652f\u6301<\/strong>\uff1aNumPy\u4e0d\u4ec5\u652f\u6301\u4e00\u7ef4\u6570\u7ec4\uff0c\u8fd8\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Python\u5185\u7f6e\u7684array\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u81ea\u5e26\u4e00\u4e2aarray\u6a21\u5757\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u5c3d\u7ba1\u529f\u80fd\u4e0d\u5982NumPy\u5f3a\u5927\uff0c\u4f46\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u503c\u6570\u7ec4\u64cd\u4f5c\u5df2\u7ecf\u8db3\u591f\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528array\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import array<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528array\u6a21\u5757\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/h2>\n<p>my_array = array.array(&#39;i&#39;, my_list)<\/p>\n<p>print(my_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>array(&#39;i&#39;, [1, 2, 3, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c&#39;i&#39;\u8868\u793a\u6570\u7ec4\u4e2d\u5b58\u50a8\u7684\u662f\u6574\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5217\u8868\u63a8\u5bfc\u5f0f\u4e3b\u8981\u7528\u4e8e\u751f\u6210\u5217\u8868\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u751f\u6210\u5176\u5b83\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u6570\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u6570\u7ec4<\/strong><\/h2>\n<p>my_array = [x for x in my_list]<\/p>\n<p>print(my_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u672c\u8d28\u4e0a\u8fd8\u662f\u751f\u6210\u4e00\u4e2a\u5217\u8868\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u4e3a\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>\u6570\u503c\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u7684\u6570\u503c\u6570\u636e\u3002NumPy\u6570\u7ec4\u7531\u4e8e\u5176\u9ad8\u6548\u7684\u8fd0\u7b97\u548c\u4e30\u5bcc\u7684\u51fd\u6570\u5e93\uff0c\u6210\u4e3a\u4e86\u6570\u636e\u79d1\u5b66\u9886\u57df\u7684\u6807\u51c6\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = np.array(list1)<\/p>\n<p>array2 = np.array(list2)<\/p>\n<h2><strong>\u6570\u7ec4\u7684\u52a0\u6cd5<\/strong><\/h2>\n<p>result_array = array1 + array2<\/p>\n<p>print(result_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>[5 7 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u901a\u5e38\u8868\u793a\u4e3a\u591a\u7ef4\u6570\u7ec4\u3002NumPy\u6570\u7ec4\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u56fe\u50cf\u6570\u636e\u7684\u64cd\u4f5c\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\u4e00\u5f20\u56fe\u7247\u5e76\u8f6c\u6362\u4e3a\u7070\u5ea6\u6a21\u5f0f<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;).convert(&#39;L&#39;)<\/p>\n<h2><strong>\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>image_array = np.array(image)<\/p>\n<p>print(image_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u9886\u57df\uff0cPandas\u5e93\u4e5f\u5e7f\u6cdb\u4f7f\u7528NumPy\u6570\u7ec4\u6765\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3, 4],<\/p>\n<p>    &#39;B&#39;: [5, 6, 7, 8]<\/p>\n<p>}<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5c06DataFrame\u4e2d\u7684\u67d0\u4e00\u5217\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>array_from_df = df[&#39;A&#39;].values<\/p>\n<p>print(array_from_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>[1 2 3 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python3\u4e2d\uff0c\u5c06\u5217\u8868\u751f\u6210\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c<strong>\u4f7f\u7528NumPy\u5e93\u7684<code>array()<\/code>\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u7528\u7684\u3002<strong>NumPy\u6570\u7ec4\u5728\u6570\u503c\u8ba1\u7b97\u3001\u56fe\u50cf\u5904\u7406\u548c\u6570\u636e\u5206\u6790\u7b49\u9886\u57df\u5177\u6709\u663e\u8457\u4f18\u52bf<\/strong>\u3002\u9664\u4e86NumPy\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684array\u6a21\u5757\uff0c\u6216\u8005\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u6570\u7ec4\u3002\u6839\u636e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python3\u4e2d\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python3\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5NumPy\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<code>pip install numpy<\/code>\u3002\u5b89\u88c5\u540e\uff0c\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmy_list = [1, 2, 3, 4]\nmy_array = np.array(my_list)\nprint(my_array)\n<\/code><\/pre>\n<p><strong>Python3\u4e2d\u4f7f\u7528\u6570\u7ec4\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528\u6570\u7ec4\u76f8\u8f83\u4e8e\u5217\u8868\u6709\u5f88\u591a\u4f18\u52bf\u3002\u6570\u7ec4\u5360\u7528\u5185\u5b58\u66f4\u5c11\uff0c\u901f\u5ea6\u66f4\u5feb\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u65f6\u3002\u6b64\u5916\uff0cNumPy\u6570\u7ec4\u652f\u6301\u591a\u7ef4\u64cd\u4f5c\u548c\u5e7f\u64ad\u529f\u80fd\uff0c\u4f7f\u5f97\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\uff0c\u65b9\u4fbf\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u7b49\u4efb\u52a1\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython3\u4e2d\u53ef\u4ee5\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u6362\u4e3a\u591a\u7ef4\u6570\u7ec4\u3002\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nnested_list = [[1, 2, 3], [4, 5, 6]]\nmulti_array = np.array(nested_list)\nprint(multi_array)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u4f1a\u5f97\u5230\u4e00\u4e2a2&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u65b9\u4fbf\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u64cd\u4f5c\u548c\u6570\u636e\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python3\u4e2d\u5c06\u5217\u8868\u751f\u6210\u6570\u7ec4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u7684array()\u51fd\u6570\u3001Python\u5185\u7f6e\u7684array\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":1123940,"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\/1123930"}],"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=1123930"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1123930\/revisions"}],"predecessor-version":[{"id":1123942,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1123930\/revisions\/1123942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1123940"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1123930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1123930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1123930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}