{"id":1163523,"date":"2025-01-15T14:56:13","date_gmt":"2025-01-15T06:56:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1163523.html"},"modified":"2025-01-15T14:56:16","modified_gmt":"2025-01-15T06:56:16","slug":"python%e6%95%b0%e7%bb%84%e5%a6%82%e4%bd%95%e7%9b%b4%e6%8e%a5%e6%b1%82%e5%b9%b3%e6%96%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1163523.html","title":{"rendered":"python\u6570\u7ec4\u5982\u4f55\u76f4\u63a5\u6c42\u5e73\u65b9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25204256\/91b8b3f8-c17c-4701-ac0f-79931555b216.webp\" alt=\"python\u6570\u7ec4\u5982\u4f55\u76f4\u63a5\u6c42\u5e73\u65b9\" \/><\/p>\n<p><p> \u4f7f\u7528Python\u76f4\u63a5\u6c42\u6570\u7ec4\u7684\u5e73\u65b9\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b8c\u6210\u3002<strong>\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4ee5\u53ca\u5faa\u73af\u904d\u5386\u6570\u7ec4\u5143\u7d20<\/strong>\u3002\u5176\u4e2d\uff0cNumPy\u5e93\u662f\u6700\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u8ba1\u7b97\u529f\u80fd\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u7684\u4e00\u79cd\u7b80\u6d01\u8868\u8fbe\u65b9\u5f0f\uff0c\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u5b83\u4e0d\u4ec5\u8bed\u6cd5\u7b80\u6d01\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u8f83\u9ad8\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u8868\u63a8\u5bfc\u5f0f\u5b9e\u73b0\u6570\u7ec4\u5e73\u65b9<\/p>\n<p>array = [1, 2, 3, 4, 5]<\/p>\n<p>squared_array = [x  2 for x in array]<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u4f7f\u7528\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u901a\u8fc7<code>x  2<\/code>\u8ba1\u7b97\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868<code>squared_array<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u8bed\u6cd5\u7b80\u6d01\uff0c\u5bb9\u6613\u7406\u89e3\u3002<\/li>\n<li>\u6267\u884c\u6548\u7387\u8f83\u9ad8\uff0c\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u5927\u578b\u6570\u7ec4\u6216\u590d\u6742\u8ba1\u7b97\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u80fd\u4e0d\u5982\u4e13\u95e8\u7684\u5e93\u9ad8\u6548\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u5b9e\u73b0\u6570\u7ec4\u5e73\u65b9<\/strong><\/h2>\n<p>array = np.array([1, 2, 3, 4, 5])<\/p>\n<p>squared_array = np.square(array)<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165NumPy\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4<code>array<\/code>\uff0c\u63a5\u7740\u4f7f\u7528<code>np.square<\/code>\u51fd\u6570\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u751f\u6210\u65b0\u7684\u6570\u7ec4<code>squared_array<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u3001\u5feb\u901f<\/strong>\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u548c\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff0c\u589e\u52a0\u4e86\u4f9d\u8d56\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u5faa\u73af\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u904d\u5386\u662f\u4e00\u79cd\u4f20\u7edf\u7684\u7f16\u7a0b\u65b9\u6cd5\uff0c\u901a\u8fc7\u904d\u5386\u6570\u7ec4\u5143\u7d20\u5e76\u9010\u4e2a\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u3002\u8fd9\u79cd\u65b9\u6cd5\u76f8\u5bf9\u7b80\u5355\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5faa\u73af\u904d\u5386\u5b9e\u73b0\u6570\u7ec4\u5e73\u65b9<\/p>\n<p>array = [1, 2, 3, 4, 5]<\/p>\n<p>squared_array = []<\/p>\n<p>for x in array:<\/p>\n<p>    squared_array.append(x  2)<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6570\u7ec4<code>array<\/code>\uff0c\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u6dfb\u52a0\u5230\u65b0\u7684\u5217\u8868<code>squared_array<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u61c2\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6548\u7387\u8f83\u4f4e<\/strong>\uff0c\u4e0d\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u867d\u7136Pandas\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u6846\u64cd\u4f5c\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528Pandas\u5b9e\u73b0\u6570\u7ec4\u5e73\u65b9<\/strong><\/h2>\n<p>array = pd.Series([1, 2, 3, 4, 5])<\/p>\n<p>squared_array = array  2<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165Pandas\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2aPandas\u7cfb\u5217\u5bf9\u8c61<code>array<\/code>\uff0c\u63a5\u7740\u5bf9\u7cfb\u5217\u5bf9\u8c61\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u751f\u6210\u65b0\u7684\u7cfb\u5217\u5bf9\u8c61<code>squared_array<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/li>\n<li>\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u548c\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u5b89\u88c5Pandas\u5e93\uff0c\u589e\u52a0\u4e86\u4f9d\u8d56\u3002<\/li>\n<li>\u76f8\u5bf9NumPy\u6765\u8bf4\uff0cPandas\u7684\u6267\u884c\u6548\u7387\u53ef\u80fd\u7565\u4f4e\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u770b\u51fa\u5728Python\u4e2d\u5bf9\u6570\u7ec4\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u9009\u3002<strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u7ec4\uff0cNumPy\u5e93\u5219\u662f\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u7684\u6700\u4f73\u9009\u62e9<\/strong>\uff0c\u5faa\u73af\u904d\u5386\u9002\u5408\u521d\u5b66\u8005\u548c\u7b80\u5355\u573a\u666f\uff0cPandas\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u6570\u7ec4\u7684\u5e73\u65b9\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u3001\u6027\u80fd\u6bd4\u8f83\u4ee5\u53ca\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8868\u8fbe\u65b9\u5f0f\uff0c\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u5b83\u4e0d\u4ec5\u8bed\u6cd5\u7b80\u6d01\uff0c\u800c\u4e14\u6267\u884c\u6548\u7387\u8f83\u9ad8\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5217\u8868\u63a8\u5bfc\u5f0f\u5b9e\u73b0\u6570\u7ec4\u5e73\u65b9<\/p>\n<p>array = [1, 2, 3, 4, 5]<\/p>\n<p>squared_array = [x  2 for x in array]<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>[x  2 for x in array]<\/code>\u8868\u793a\u5bf9<code>array<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20<code>x<\/code>\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868<code>squared_array<\/code>\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6027\u80fd\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u6267\u884c\u6548\u7387\u8f83\u9ad8\uff0c\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u7ec4\u3002\u5176\u6027\u80fd\u4f18\u4e8e\u5faa\u73af\u904d\u5386\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982NumPy\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u6d4b\u8bd5\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u6027\u80fd<\/strong><\/h2>\n<p>array = list(range(1000000))<\/p>\n<p>start_time = time.time()<\/p>\n<p>squared_array = [x  2 for x in array]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;\u5217\u8868\u63a8\u5bfc\u5f0f\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u53ef\u4ee5\u6d4b\u8bd5\u5217\u8868\u63a8\u5bfc\u5f0f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\u7684\u6027\u80fd\u8868\u73b0\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u9002\u5408\u4ee5\u4e0b\u4f7f\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u7ec4\u65f6\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u63d0\u4f9b\u7b80\u6d01\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/li>\n<li>\u5728\u9700\u8981\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u7b80\u5355\u8ba1\u7b97\u65f6\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u65b0\u7684\u5217\u8868\u3002<\/li>\n<li>\u5728\u7f16\u5199\u7b80\u6d01\u3001\u53ef\u8bfb\u6027\u9ad8\u7684\u4ee3\u7801\u65f6\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001NumPy\u5e93<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u5b9e\u73b0\u6570\u7ec4\u5e73\u65b9<\/strong><\/h2>\n<p>array = np.array([1, 2, 3, 4, 5])<\/p>\n<p>squared_array = np.square(array)<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165NumPy\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4<code>array<\/code>\uff0c\u63a5\u7740\u4f7f\u7528<code>np.square<\/code>\u51fd\u6570\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u751f\u6210\u65b0\u7684\u6570\u7ec4<code>squared_array<\/code>\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6027\u80fd\u5206\u6790<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\uff0c\u6027\u80fd\u975e\u5e38\u51fa\u8272\u3002\u76f8\u6bd4\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5faa\u73af\u904d\u5386\uff0cNumPy\u7684\u6267\u884c\u6548\u7387\u8981\u9ad8\u5f97\u591a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u6d4b\u8bd5NumPy\u7684\u6027\u80fd<\/strong><\/h2>\n<p>array = np.arange(1000000)<\/p>\n<p>start_time = time.time()<\/p>\n<p>squared_array = np.square(array)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;NumPy\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u53ef\u4ee5\u6d4b\u8bd5NumPy\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\u7684\u6027\u80fd\u8868\u73b0\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u9002\u5408\u4ee5\u4e0b\u4f7f\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4<\/strong>\u65f6\uff0cNumPy\u53ef\u4ee5\u63d0\u4f9b\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/li>\n<li>\u5728\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8ba1\u7b97\u548c\u6570\u7ec4\u64cd\u4f5c\u65f6\uff0cNumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u3002<\/li>\n<li>\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u9886\u57df\uff0cNumPy\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\u3002<\/li>\n<\/ul>\n<p><h2>\u4e09\u3001\u5faa\u73af\u904d\u5386<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u904d\u5386\u662f\u4e00\u79cd\u4f20\u7edf\u7684\u7f16\u7a0b\u65b9\u6cd5\uff0c\u901a\u8fc7\u904d\u5386\u6570\u7ec4\u5143\u7d20\u5e76\u9010\u4e2a\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u3002\u8fd9\u79cd\u65b9\u6cd5\u76f8\u5bf9\u7b80\u5355\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5faa\u73af\u904d\u5386\u5b9e\u73b0\u6570\u7ec4\u5e73\u65b9<\/p>\n<p>array = [1, 2, 3, 4, 5]<\/p>\n<p>squared_array = []<\/p>\n<p>for x in array:<\/p>\n<p>    squared_array.append(x  2)<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6570\u7ec4<code>array<\/code>\uff0c\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u6dfb\u52a0\u5230\u65b0\u7684\u5217\u8868<code>squared_array<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6027\u80fd\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u904d\u5386\u7684\u6267\u884c\u6548\u7387\u8f83\u4f4e\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\uff0c\u6027\u80fd\u8f83\u5dee\u3002\u76f8\u6bd4\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\u548cNumPy\uff0c\u5faa\u73af\u904d\u5386\u7684\u6027\u80fd\u6700\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u6d4b\u8bd5\u5faa\u73af\u904d\u5386\u7684\u6027\u80fd<\/strong><\/h2>\n<p>array = list(range(1000000))<\/p>\n<p>start_time = time.time()<\/p>\n<p>squared_array = []<\/p>\n<p>for x in array:<\/p>\n<p>    squared_array.append(x  2)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;\u5faa\u73af\u904d\u5386\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u53ef\u4ee5\u6d4b\u8bd5\u5faa\u73af\u904d\u5386\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\u7684\u6027\u80fd\u8868\u73b0\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u904d\u5386\u9002\u5408\u4ee5\u4e0b\u4f7f\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u521d\u5b66\u8005\u5b66\u4e60<\/strong>\u65f6\uff0c\u5faa\u73af\u904d\u5386\u662f\u4e00\u79cd\u7b80\u5355\u6613\u61c2\u7684\u7f16\u7a0b\u65b9\u6cd5\u3002<\/li>\n<li>\u5728\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u7ec4\u6216\u7b80\u5355\u8ba1\u7b97\u65f6\uff0c\u5faa\u73af\u904d\u5386\u53ef\u4ee5\u63d0\u4f9b\u76f4\u63a5\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/li>\n<li>\u5728\u9700\u8981\u9010\u6b65\u7406\u89e3\u548c\u4f18\u5316\u4ee3\u7801\u65f6\uff0c\u5faa\u73af\u904d\u5386\u662f\u4e00\u79cd\u57fa\u7840\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<p><h2>\u56db\u3001Pandas\u5e93<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u867d\u7136Pandas\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u6846\u64cd\u4f5c\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528Pandas\u5b9e\u73b0\u6570\u7ec4\u5e73\u65b9<\/strong><\/h2>\n<p>array = pd.Series([1, 2, 3, 4, 5])<\/p>\n<p>squared_array = array  2<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165Pandas\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2aPandas\u7cfb\u5217\u5bf9\u8c61<code>array<\/code>\uff0c\u63a5\u7740\u5bf9\u7cfb\u5217\u5bf9\u8c61\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u751f\u6210\u65b0\u7684\u7cfb\u5217\u5bf9\u8c61<code>squared_array<\/code>\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6027\u80fd\u5206\u6790<\/h3>\n<\/p>\n<p><p>Pandas\u5728\u5904\u7406\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u4f46\u76f8\u6bd4\u4e8eNumPy\uff0cPandas\u7684\u6267\u884c\u6548\u7387\u53ef\u80fd\u7565\u4f4e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u6d4b\u8bd5Pandas\u7684\u6027\u80fd<\/strong><\/h2>\n<p>array = pd.Series(range(1000000))<\/p>\n<p>start_time = time.time()<\/p>\n<p>squared_array = array  2<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;Pandas\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u53ef\u4ee5\u6d4b\u8bd5Pandas\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\u7684\u6027\u80fd\u8868\u73b0\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u9002\u5408\u4ee5\u4e0b\u4f7f\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u5206\u6790\u548c\u5904\u7406<\/strong>\u65f6\uff0cPandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u4fbf\u6377\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u5728\u9700\u8981\u5904\u7406\u591a\u79cd\u6570\u636e\u683c\u5f0f\u548c\u590d\u6742\u6570\u636e\u64cd\u4f5c\u65f6\uff0cPandas\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<\/li>\n<li>\u5728\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u8f6c\u6362\u548c\u53ef\u89c6\u5316\u65f6\uff0cPandas\u53ef\u4ee5\u63d0\u4f9b\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/li>\n<\/ul>\n<p><h2>\u4e94\u3001\u6df1\u5165\u63a2\u8ba8\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9<\/h2>\n<\/p>\n<p><h3>1\u3001\u5217\u8868\u63a8\u5bfc\u5f0f vs \u5faa\u73af\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5faa\u73af\u904d\u5386\u90fd\u662fPython\u4e2d\u7684\u57fa\u672c\u7f16\u7a0b\u65b9\u6cd5\uff0c\u4f46\u5b83\u4eec\u5728\u8bed\u6cd5\u7b80\u6d01\u6027\u548c\u6267\u884c\u6548\u7387\u4e0a\u5b58\u5728\u663e\u8457\u5dee\u5f02\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u8bed\u6cd5\u66f4\u7b80\u6d01\uff0c\u6267\u884c\u6548\u7387\u4e5f\u66f4\u9ad8\uff0c\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u7ec4\u548c\u7b80\u5355\u8ba1\u7b97\u3002\u800c\u5faa\u73af\u904d\u5386\u5219\u66f4\u9002\u5408\u521d\u5b66\u8005\u7406\u89e3\u548c\u4f7f\u7528\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><h3>2\u3001NumPy vs Pandas<\/h3>\n<\/p>\n<p><p>NumPy\u548cPandas\u90fd\u662fPython\u4e2d\u5e38\u7528\u7684\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u5e93\uff0c\u4f46\u5b83\u4eec\u7684\u4fa7\u91cd\u70b9\u4e0d\u540c\u3002NumPy\u4e13\u6ce8\u4e8e\u9ad8\u6548\u7684\u6570\u7ec4\u8ba1\u7b97\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u548c\u590d\u6742\u6570\u5b66\u8fd0\u7b97\u3002Pandas\u5219\u4fa7\u91cd\u4e8e\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u591a\u79cd\u6570\u636e\u683c\u5f0f\u548c\u590d\u6742\u6570\u636e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u8003\u8651\u5177\u4f53\u7684\u4f7f\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u5982\u679c\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u7ec4\u548c\u7b80\u5355\u8ba1\u7b97\uff0c\u53ef\u4ee5\u9009\u62e9\u5217\u8868\u63a8\u5bfc\u5f0f\uff1b\u5982\u679c\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u548c\u590d\u6742\u8fd0\u7b97\uff0cNumPy\u662f\u6700\u4f73\u9009\u62e9\uff1b\u5982\u679c\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\uff0cPandas\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff1b\u5982\u679c\u662f\u521d\u5b66\u8005\u5b66\u4e60\u548c\u7406\u89e3\u57fa\u672c\u7f16\u7a0b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9009\u62e9\u5faa\u73af\u904d\u5386\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5bf9\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\u548c\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u6570\u636e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u9884\u5904\u7406\u9636\u6bb5\uff0c\u5e38\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u8f6c\u6362\u548c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5bf9\u6570\u636e\u4e2d\u7684\u6570\u503c\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u6216Pandas\u8fdb\u884c\u9ad8\u6548\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406<\/strong><\/h2>\n<p>data = np.array([1, 2, 3, 4, 5])<\/p>\n<p>processed_data = np.square(data)<\/p>\n<h2><strong>\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406<\/strong><\/h2>\n<p>data = pd.Series([1, 2, 3, 4, 5])<\/p>\n<p>processed_data = data  2<\/p>\n<p>print(processed_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u79d1\u5b66\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u9886\u57df\uff0c\u5e38\u5e38\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u548c\u6570\u7ec4\u64cd\u4f5c\u3002NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u548c\u9ad8\u6548\u7684\u6570\u7ec4\u8ba1\u7b97\u529f\u80fd\uff0c\u662f\u79d1\u5b66\u8ba1\u7b97\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97<\/strong><\/h2>\n<p>data = np.arange(1000000)<\/p>\n<p>squared_data = np.square(data)<\/p>\n<p>print(squared_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u6570\u636e\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u9886\u57df\uff0cPandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u529f\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u3001\u8f6c\u6362\u548c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5206\u6790<\/strong><\/h2>\n<p>data = pd.Series(range(1000000))<\/p>\n<p>squared_data = data  2<\/p>\n<p>print(squared_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u6d4b\u8bd5\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd<\/strong><\/h2>\n<p>data = range(1000000)<\/p>\n<h2><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>squared_data = [x  2 for x in data]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;\u5217\u8868\u63a8\u5bfc\u5f0f\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<h2><strong>NumPy<\/strong><\/h2>\n<p>data = np.arange(1000000)<\/p>\n<p>start_time = time.time()<\/p>\n<p>squared_data = np.square(data)<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;NumPy\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<h2><strong>Pandas<\/strong><\/h2>\n<p>data = pd.Series(range(1000000))<\/p>\n<p>start_time = time.time()<\/p>\n<p>squared_data = data  2<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;Pandas\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u53ef\u4ee5\u6d4b\u8bd5\u4e0d\u540c\u65b9\u6cd5\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\u7684\u6027\u80fd\u8868\u73b0\uff0c\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u6027\u80fd\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u7ed3\u8bba<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u770b\u51fa\u5728Python\u4e2d\u5bf9\u6570\u7ec4\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u9009\u3002<strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u7ec4\uff0cNumPy\u5e93\u662f\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u7684\u6700\u4f73\u9009\u62e9\uff0c\u5faa\u73af\u904d\u5386\u9002\u5408\u521d\u5b66\u8005\u548c\u7b80\u5355\u573a\u666f\uff0cPandas\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u5904\u7406<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u6570\u7ec4\u7684\u5e73\u65b9\u8fd0\u7b97\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u548c\u6027\u80fd\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728Python\u4e2d\u5904\u7406\u6570\u7ec4\u5e73\u65b9\u8fd0\u7b97\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u5982\u4f55\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u8f7b\u677e\u5b9e\u73b0\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u8fd0\u7b97\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff08\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install numpy<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u7740\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4\u5e76\u4f7f\u7528<code>numpy.square()<\/code>\u51fd\u6570\u6765\u83b7\u5f97\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\narray = np.array([1, 2, 3, 4])\nsquared_array = np.square(array)\nprint(squared_array)  # \u8f93\u51fa: [ 1  4  9 16]\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5bf9Python\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5217\u8868\u3002\u5728\u5bf9\u5217\u8868\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [1, 2, 3, 4]\nsquared_list = [x**2 for x in my_list]\nprint(squared_list)  # \u8f93\u51fa: [1, 4, 9, 16]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u65f6\u975e\u5e38\u9ad8\u6548\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5bf9\u5927\u6570\u7ec4\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u4ee5\u63d0\u9ad8\u6027\u80fd\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5927\u6570\u7ec4\u800c\u8a00\uff0c\u4f7f\u7528NumPy\u5e93\u4e0d\u4ec5\u80fd\u591f\u5b9e\u73b0\u9ad8\u6548\u7684\u5e73\u65b9\u8fd0\u7b97\uff0c\u8fd8\u80fd\u5229\u7528\u5176\u5411\u91cf\u5316\u64cd\u4f5c\u6765\u52a0\u901f\u8ba1\u7b97\u3002NumPy\u7684\u5e95\u5c42\u5b9e\u73b0\u662f\u7528C\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u901f\u5ea6\u8fdc\u8fdc\u5feb\u4e8e\u5e38\u89c4\u7684Python\u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nlarge_array = np.random.rand(1000000)  # \u751f\u6210\u4e00\u4e2a\u5305\u542b100\u4e07\u4e2a\u968f\u673a\u6570\u7684\u6570\u7ec4\nsquared_large_array = large_array ** 2\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u6709\u6548\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c\u51cf\u5c11\u8ba1\u7b97\u65f6\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u76f4\u63a5\u6c42\u6570\u7ec4\u7684\u5e73\u65b9\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b8c\u6210\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4ee5\u53ca\u5faa\u73af [&hellip;]","protected":false},"author":3,"featured_media":1163529,"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\/1163523"}],"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=1163523"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163523\/revisions"}],"predecessor-version":[{"id":1163533,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163523\/revisions\/1163533"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1163529"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1163523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1163523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1163523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}