{"id":955642,"date":"2024-12-27T02:19:49","date_gmt":"2024-12-26T18:19:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/955642.html"},"modified":"2024-12-27T02:19:52","modified_gmt":"2024-12-26T18:19:52","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%a3%b0%e6%98%8e%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/955642.html","title":{"rendered":"\u5982\u4f55\u7528python\u58f0\u660e\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095534\/cd00a2f7-9e48-40f3-b66f-a017d69723cd.webp\" alt=\"\u5982\u4f55\u7528python\u58f0\u660e\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u58f0\u660e\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5217\u8868(List)\u3001NumPy\u6570\u7ec4\u548c\u6570\u7ec4\u6a21\u5757\uff08array module\uff09\u3002\u5217\u8868\u662fPython\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u652f\u6301\u52a8\u6001\u5927\u5c0f\u8c03\u6574\u548c\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff1bNumPy\u6570\u7ec4\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u64cd\u4f5c\u80fd\u529b\uff0c\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\uff1b\u6570\u7ec4\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u4e8eC\u8bed\u8a00\u7684\u6570\u7ec4\u7ed3\u6784\uff0c\u9002\u5408\u5904\u7406\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3002\u901a\u5e38\uff0c\u4f7f\u7528\u5217\u8868\u548cNumPy\u6570\u7ec4\u662f\u6700\u5e38\u89c1\u548c\u63a8\u8350\u7684\u65b9\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u662f\u58f0\u660e\u6570\u7ec4\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u4e00\u822c\u7528\u9014\u3002NumPy\u6570\u7ec4\u5219\u66f4\u9002\u5408\u9700\u8981\u9ad8\u6027\u80fd\u548c\u591a\u7ef4\u6570\u7ec4\u7684\u573a\u666f\uff0c\u5982\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u548c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5217\u8868\uff08LIST\uff09<\/p>\n<\/p>\n<p><p>\u5217\u8868\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u7ec4\u7ed3\u6784\uff0c\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\u548c\u65b9\u4fbf\u6027\u3002\u5217\u8868\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5e76\u4e14\u652f\u6301\u52a8\u6001\u8c03\u6574\u5927\u5c0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u5217\u8868<\/p>\n<p>\u5728Python\u4e2d\u521b\u5efa\u5217\u8868\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u65b9\u62ec\u53f7<code>[]<\/code>\u6765\u5305\u542b\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5b58\u50a8\u5c11\u91cf\u6570\u636e\u6216\u6570\u636e\u7c7b\u578b\u8f83\u4e3a\u6df7\u5408\u7684\u60c5\u51b5\u3002\u7531\u4e8e\u5217\u8868\u662f\u52a8\u6001\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u968f\u65f6\u6dfb\u52a0\u3001\u5220\u9664\u6216\u4fee\u6539\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u5217\u8868\u64cd\u4f5c<\/p>\n<p>Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5217\u8868\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>.append()<\/code>\u53ef\u4ee5\u6dfb\u52a0\u5143\u7d20\uff0c\u4f7f\u7528<code>.remove()<\/code>\u53ef\u4ee5\u5220\u9664\u5143\u7d20\uff0c\u4f7f\u7528<code>len()<\/code>\u53ef\u4ee5\u83b7\u53d6\u5217\u8868\u7684\u957f\u5ea6\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">my_list.append(6)<\/p>\n<p>my_list.remove(2)<\/p>\n<p>length = len(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u8fd8\u652f\u6301\u5207\u7247\u64cd\u4f5c\uff0c\u5141\u8bb8\u83b7\u53d6\u5b50\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>my_list[1:3]<\/code>\u53ef\u4ee5\u83b7\u53d6\u5217\u8868\u7684\u7b2c\u4e8c\u4e2a\u548c\u7b2c\u4e09\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u6570\u7ec4<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u5b66\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61ndarray\u3002NumPy\u6570\u7ec4\u5728\u6267\u884c\u5927\u91cf\u6570\u5b66\u8fd0\u7b97\u65f6\u6027\u80fd\u4f18\u8d8a\uff0c\u56e0\u6b64\u5728\u6570\u636e\u5206\u6790\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5NumPy<\/p>\n<p>\u9996\u5148\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/li>\n<\/p>\n<\/ol>\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<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.array([1, 2, 3, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u652f\u6301\u591a\u79cd\u521b\u5efa\u65b9\u5f0f\uff0c\u5305\u62ec\u4f7f\u7528<code>np.zeros()<\/code>\u521b\u5efa\u5168\u96f6\u6570\u7ec4\uff0c\u4f7f\u7528<code>np.ones()<\/code>\u521b\u5efa\u5168\u4e00\u6570\u7ec4\uff0c\u4ee5\u53ca\u4f7f\u7528<code>np.arange()<\/code>\u521b\u5efa\u7b49\u5dee\u6570\u5217\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>NumPy\u6570\u7ec4\u64cd\u4f5c<\/p>\n<p>NumPy\u6570\u7ec4\u652f\u6301\u591a\u79cd\u6570\u7ec4\u64cd\u4f5c\uff0c\u5305\u62ec\u6570\u7ec4\u5f62\u72b6\u53d8\u6362\u3001\u6570\u5b66\u8fd0\u7b97\u3001\u7d22\u5f15\u548c\u5207\u7247\u7b49\u3002\u4f8b\u5982\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6570\u7ec4\u5f62\u72b6\u53d8\u6362<\/p>\n<p>reshaped_array = my_array.reshape((5, 1))<\/p>\n<h2><strong>\u6570\u5b66\u8fd0\u7b97<\/strong><\/h2>\n<p>sum_array = my_array + 10<\/p>\n<h2><strong>\u7d22\u5f15\u548c\u5207\u7247<\/strong><\/h2>\n<p>sub_array = my_array[1:4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u6570\u5b66\u51fd\u6570\u548c\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u79d1\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u6570\u7ec4\u6a21\u5757\uff08ARRAY MODULE\uff09<\/p>\n<\/p>\n<p><p>\u6570\u7ec4\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u7c7b\u4f3c\u4e8eC\u8bed\u8a00\u6570\u7ec4\u7684\u7ed3\u6784\uff0c\u9002\u5408\u5904\u7406\u56fa\u5b9a\u7c7b\u578b\u7684\u6570\u636e\u3002\u5176\u6027\u80fd\u4f18\u4e8e\u5217\u8868\uff0c\u4f46\u7075\u6d3b\u6027\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165\u6570\u7ec4\u6a21\u5757<\/p>\n<p>\u4f7f\u7528\u6570\u7ec4\u6a21\u5757\u9700\u8981\u5148\u5bfc\u5165<code>array<\/code>\u5e93\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">import array<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa\u6570\u7ec4<\/p>\n<p>\u521b\u5efa\u6570\u7ec4\u65f6\u9700\u8981\u6307\u5b9a\u6570\u636e\u7c7b\u578b\u548c\u521d\u59cb\u503c\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6574\u6570\u7684\u6570\u7ec4\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">int_array = array.array(&#39;i&#39;, [1, 2, 3, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>&#39;i&#39;<\/code>\u8868\u793a\u6574\u6570\u7c7b\u578b\u3002\u6570\u7ec4\u6a21\u5757\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u5b57\u7b26\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u7b49\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u6570\u7ec4\u64cd\u4f5c<\/p>\n<p>\u6570\u7ec4\u6a21\u5757\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4f8b\u5982\u6dfb\u52a0\u3001\u5220\u9664\u548c\u83b7\u53d6\u5143\u7d20\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\">int_array.append(6)<\/p>\n<p>int_array.remove(2)<\/p>\n<p>element = int_array[2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6570\u7ec4\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u76f8\u540c\u7c7b\u578b\u6570\u636e\u4e14\u4e0d\u9700\u8981\u52a8\u6001\u8c03\u6574\u5927\u5c0f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u6570\u7ec4\u7c7b\u578b<\/p>\n<\/p>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u7ec4\u7c7b\u578b\u662f\u4f18\u5316\u6027\u80fd\u548c\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u7684\u5173\u952e\u3002\u5728\u5904\u7406\u5c11\u91cf\u6570\u636e\u6216\u6570\u636e\u7c7b\u578b\u4e0d\u56fa\u5b9a\u7684\u60c5\u51b5\u4e0b\uff0c\u5217\u8868\u662f\u6700\u5408\u9002\u7684\u9009\u62e9\uff1b\u5728\u9700\u8981\u9ad8\u6548\u5904\u7406\u5927\u91cf\u6570\u503c\u6570\u636e\u65f6\uff0cNumPy\u6570\u7ec4\u662f\u6700\u4f73\u9009\u62e9\uff1b\u5982\u679c\u9700\u8981\u56fa\u5b9a\u7c7b\u578b\u7684\u6570\u7ec4\u4e14\u4e0d\u9700\u8981\u52a8\u6001\u8c03\u6574\u5927\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u6a21\u5757\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u5217\u8868\u9002\u7528\u573a\u666f<\/p>\n<p>\u5217\u8868\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u6216\u5b58\u50a8\u6df7\u5408\u7c7b\u578b\u6570\u636e\u7684\u60c5\u51b5\u3002\u5176\u64cd\u4f5c\u7b80\u5355\uff0c\u9002\u5408\u521d\u5b66\u8005\u548c\u4e0d\u9700\u8981\u9ad8\u6027\u80fd\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>NumPy\u6570\u7ec4\u9002\u7528\u573a\u666f<\/p>\n<p>NumPy\u6570\u7ec4\u662f\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7684\u9996\u9009\u3002\u5176\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u64cd\u4f5c\u80fd\u529b\uff0c\u80fd\u591f\u663e\u8457\u63d0\u5347\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u6570\u7ec4\u6a21\u5757\u9002\u7528\u573a\u666f<\/p>\n<p>\u6570\u7ec4\u6a21\u5757\u9002\u5408\u5904\u7406\u56fa\u5b9a\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5176\u6027\u80fd\u4f18\u4e8e\u5217\u8868\uff0c\u4f46\u7075\u6d3b\u6027\u8f83\u4f4e\uff0c\u9002\u5408\u9ad8\u6027\u80fd\u9700\u6c42\u7684\u7279\u5b9a\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6570\u7ec4\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u7ec4\u64cd\u4f5c\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u90e8\u5206\u3002\u65e0\u8bba\u662f\u6570\u636e\u5206\u6790\u3001\u673a\u5668\u5b66\u4e60\uff0c\u8fd8\u662f\u65e5\u5e38\u7f16\u7a0b\u4efb\u52a1\uff0c\u6570\u7ec4\u7684\u4f7f\u7528\u90fd\u662f\u975e\u5e38\u666e\u904d\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6570\u636e\u5206\u6790\u4e2d\u7684\u6570\u7ec4\u64cd\u4f5c<\/p>\n<p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3001\u7edf\u8ba1\u548c\u53ef\u89c6\u5316\u3002NumPy\u6570\u7ec4\u5728\u8fd9\u65b9\u9762\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u6570\u7ec4\u5bf9\u6570\u636e\u8fdb\u884c\u6807\u51c6\u5316\u3001\u5f52\u4e00\u5316\u3001\u8ba1\u7b97\u5747\u503c\u548c\u65b9\u5dee\u7b49\u64cd\u4f5c\u3002<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6807\u51c6\u5316<\/p>\n<p>mean = np.mean(my_array)<\/p>\n<p>std_dev = np.std(my_array)<\/p>\n<p>standardized_array = (my_array - mean) \/ std_dev<\/p>\n<h2><strong>\u5f52\u4e00\u5316<\/strong><\/h2>\n<p>normalized_array = (my_array - np.min(my_array)) \/ (np.max(my_array) - np.min(my_array))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u673a\u5668\u5b66\u4e60\u4e2d\u7684\u6570\u7ec4\u64cd\u4f5c<\/p>\n<p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u77e9\u9635\u5f62\u5f0f\u8868\u793a\uff0cNumPy\u6570\u7ec4\u662f\u5b58\u50a8\u548c\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u7684\u7406\u60f3\u9009\u62e9\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u6570\u7ec4\u8868\u793a\u7279\u5f81\u77e9\u9635\u548c\u6807\u7b7e\u5411\u91cf\uff0c\u5e76\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\u7b49\u64cd\u4f5c\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u7279\u5f81\u77e9\u9635\u548c\u6807\u7b7e\u5411\u91cf<\/p>\n<p>features = np.array([[1, 2], [3, 4], [5, 6]])<\/p>\n<p>labels = np.array([0, 1, 0])<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>result = np.dot(features, labels)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u65e5\u5e38\u7f16\u7a0b\u4efb\u52a1\u4e2d\u7684\u6570\u7ec4\u64cd\u4f5c<\/p>\n<p>\u5728\u65e5\u5e38\u7f16\u7a0b\u4efb\u52a1\u4e2d\uff0c\u6570\u7ec4\u64cd\u4f5c\u4e5f\u975e\u5e38\u5e38\u89c1\u3002\u4f8b\u5982\uff0c\u5b58\u50a8\u548c\u5904\u7406\u4e00\u7ec4\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u6216\u5bf9\u8c61\u65f6\uff0c\u5217\u8868\u662f\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff1a<\/li>\n<\/p>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u5b57\u7b26\u4e32\u5217\u8868<\/p>\n<p>str_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<h2><strong>\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20<\/strong><\/h2>\n<p>str_list.append(&#39;date&#39;)<\/p>\n<p>str_list.remove(&#39;banana&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u58f0\u660e\u6570\u7ec4\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u5217\u8868\u3001NumPy\u6570\u7ec4\u548c\u6570\u7ec4\u6a21\u5757\u3002\u9009\u62e9\u5408\u9002\u7684\u6570\u7ec4\u7c7b\u578b\u80fd\u591f\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\uff0c\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5217\u8868\u9002\u5408\u4e00\u822c\u7528\u9014\u548c\u52a8\u6001\u6570\u7ec4\u9700\u6c42\uff0cNumPy\u6570\u7ec4\u9002\u5408\u9ad8\u6027\u80fd\u6570\u503c\u8ba1\u7b97\uff0c\u6570\u7ec4\u6a21\u5757\u9002\u5408\u5904\u7406\u56fa\u5b9a\u7c7b\u578b\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5408\u7406\u4f7f\u7528\u6570\u7ec4\u64cd\u4f5c\u80fd\u591f\u6709\u6548\u89e3\u51b3\u6570\u636e\u5904\u7406\u3001\u5206\u6790\u548c\u8ba1\u7b97\u95ee\u9898\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u4e2d\u7684\u6570\u7ec4\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\u867d\u7136\u6ca1\u6709\u5185\u7f6e\u7684\u6570\u7ec4\u7c7b\u578b\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\uff08list\uff09\u6765\u521b\u5efa\u7c7b\u4f3c\u6570\u7ec4\u7684\u7ed3\u6784\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u65b9\u62ec\u53f7\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\uff0c\u4f8b\u5982\uff1a<code>my_array = [1, 2, 3, 4, 5]<\/code>\u3002\u5bf9\u4e8e\u9700\u8981\u66f4\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u3002\u4f7f\u7528NumPy\u521b\u5efa\u6570\u7ec4\u7684\u65b9\u5f0f\u662f\uff1a<code>import numpy as np<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>np.array([1, 2, 3, 4, 5])<\/code>\u6765\u58f0\u660e\u6570\u7ec4\u3002<\/p>\n<p><strong>Python\u6570\u7ec4\u548c\u5217\u8868\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>Python\u7684\u5217\u8868\u662f\u4e00\u79cd\u53ef\u4ee5\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u5143\u7d20\u7684\u7075\u6d3b\u6570\u636e\u7ed3\u6784\uff0c\u800c\u6570\u7ec4\u4e3b\u8981\u662f\u7528\u4e8e\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\u3002\u4f7f\u7528NumPy\u6570\u7ec4\u65f6\uff0c\u6240\u6709\u5143\u7d20\u5fc5\u987b\u662f\u76f8\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u4f7f\u5f97\u6570\u7ec4\u5728\u6570\u503c\u8ba1\u7b97\u65f6\u66f4\u9ad8\u6548\u3002\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u503c\u6570\u636e\u7684\u573a\u666f\uff0cNumPy\u6570\u7ec4\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bbf\u95ee\u6570\u7ec4\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u65e0\u8bba\u662f\u4f7f\u7528\u5217\u8868\u8fd8\u662fNumPy\u6570\u7ec4\uff0c\u8bbf\u95ee\u5143\u7d20\u7684\u65b9\u6cd5\u90fd\u5f88\u7b80\u5355\u3002\u5bf9\u4e8e\u5217\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\uff0c\u4f8b\u5982\uff1a<code>my_array[0]<\/code>\u5c06\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u5728NumPy\u6570\u7ec4\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u7d22\u5f15\u65b9\u6cd5\uff0c\u6bd4\u5982\uff1a<code>np_array[0]<\/code>\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u8bbf\u95ee\u5143\u7d20\u65f6\u53ef\u4ee5\u4f7f\u7528\u9017\u53f7\u5206\u9694\u7684\u7d22\u5f15\uff0c\u4f8b\u5982\uff1a<code>np_array[0, 1]<\/code>\u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u7075\u6d3b\u7684\u7d22\u5f15\u65b9\u5f0f\u4f7f\u5f97\u6570\u7ec4\u64cd\u4f5c\u66f4\u52a0\u76f4\u89c2\u548c\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u58f0\u660e\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5217\u8868(List)\u3001NumPy\u6570\u7ec4\u548c\u6570\u7ec4\u6a21\u5757\uff08 [&hellip;]","protected":false},"author":3,"featured_media":955645,"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\/955642"}],"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=955642"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/955642\/revisions"}],"predecessor-version":[{"id":955647,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/955642\/revisions\/955647"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/955645"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=955642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=955642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=955642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}