{"id":1060571,"date":"2024-12-31T15:35:10","date_gmt":"2024-12-31T07:35:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1060571.html"},"modified":"2024-12-31T15:35:12","modified_gmt":"2024-12-31T07:35:12","slug":"python-%e5%a6%82%e4%bd%95%e5%b9%b3%e6%96%b9%e4%b8%80%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1060571.html","title":{"rendered":"python \u5982\u4f55\u5e73\u65b9\u4e00\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5d7ae9d7-aa37-4d18-9ea5-2270e699b5e7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python \u5982\u4f55\u5e73\u65b9\u4e00\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>Python\u5e73\u65b9\u4e00\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u4e58\u6cd5\u8fd0\u7b97\u7b26\u3001\u5185\u7f6e\u7684<code>pow<\/code>\u51fd\u6570\u3001\u4ee5\u53caNumPy\u5e93\u4e2d\u7684<code>square<\/code>\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528\u4e58\u6cd5\u8fd0\u7b97\u7b26\uff0c\u56e0\u4e3a\u5b83\u65e2\u7b80\u5355\u53c8\u9ad8\u6548\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u4e58\u6cd5\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>\u8fd9\u662f\u6700\u7b80\u5355\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u4e00\u4e2a\u6570\u4e58\u4ee5\u5b83\u672c\u8eab\uff0c\u5373\u53ef\u5f97\u5230\u5e73\u65b9\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 5<\/p>\n<p>square = number * number<\/p>\n<p>print(square)  # \u8f93\u51fa25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u76f4\u89c2\u3001\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u5408\u4efb\u4f55\u7b80\u5355\u7684\u8ba1\u7b97\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u7684<code>pow<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684<code>pow<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u4efb\u610f\u6570\u7684\u5e42\uff0c\u5305\u62ec\u5e73\u65b9\u3002<code>pow<\/code>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5e95\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6307\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 5<\/p>\n<p>square = pow(number, 2)<\/p>\n<p>print(square)  # \u8f93\u51fa25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong><code>pow<\/code>\u51fd\u6570\u7684\u4f18\u70b9\u662f\u901a\u7528\u6027\u5f3a\uff0c\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u4efb\u610f\u6b21\u5e42\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u5e73\u65b9\u3002<\/strong> \u5b83\u5728\u9700\u8981\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7684\u6570\u5b66\u51fd\u6570\uff0c\u5305\u62ec<code>numpy.square<\/code>\uff0c\u7528\u4e8e\u8ba1\u7b97\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5NumPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u6765\u8ba1\u7b97\u5e73\u65b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>number = 5<\/p>\n<p>square = np.square(number)<\/p>\n<p>print(square)  # \u8f93\u51fa25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>NumPy\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u7684\u9ad8\u6548\u6027\u548c\u7075\u6d3b\u6027\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002<\/strong> \u5982\u679c\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u503c\u8ba1\u7b97\uff0c\u63a8\u8350\u4f7f\u7528NumPy\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5217\u8868\u6216\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6216\u751f\u6210\u5668\u6765\u9ad8\u6548\u5730\u8ba1\u7b97\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squares = [x * x for x in numbers]<\/p>\n<p>print(squares)  # \u8f93\u51fa [1, 4, 9, 16, 25]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u4f18\u96c5\uff0c\u9002\u5408\u5904\u7406\u4e00\u7ef4\u6570\u7ec4\u6216\u5217\u8868\u7684\u5e73\u65b9\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6269\u5c55\uff1a\u7ed3\u5408\u6570\u5b66\u51fd\u6570\u548c\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e73\u65b9\u64cd\u4f5c\u5f80\u5f80\u4f1a\u4e0e\u5176\u4ed6\u6570\u5b66\u51fd\u6570\u548c\u6570\u636e\u7ed3\u6784\u7ed3\u5408\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8ba1\u7b97\uff0c\u7136\u540e\u8fdb\u884c\u5176\u4ed6\u7edf\u8ba1\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed3\u5408\u5e73\u65b9\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7684\u793a\u4f8b\uff1a<\/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>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;B&#39;: [2, 3, 4, 5, 6]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5bf9\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8ba1\u7b97<\/strong><\/h2>\n<p>df_squared = df.applymap(lambda x: x  2)<\/p>\n<p>print(df_squared)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Pandas\u5e93\u6765\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u96c6\uff0c\u5e76\u4f7f\u7528<code>applymap<\/code>\u65b9\u6cd5\u5bf9\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8ba1\u7b97\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5e73\u65b9\u8ba1\u7b97\u7684\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u91cd\u8981\u8003\u8651\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u5bf9\u51e0\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\u6bd4\u8f83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>number = 1000<\/p>\n<h2><strong>\u4f7f\u7528\u4e58\u6cd5\u8fd0\u7b97\u7b26<\/strong><\/h2>\n<p>time1 = timeit.timeit(&#39;number * number&#39;, globals=globals(), number=1000000)<\/p>\n<h2><strong>\u4f7f\u7528pow\u51fd\u6570<\/strong><\/h2>\n<p>time2 = timeit.timeit(&#39;pow(number, 2)&#39;, globals=globals(), number=1000000)<\/p>\n<h2><strong>\u4f7f\u7528NumPy<\/strong><\/h2>\n<p>time3 = timeit.timeit(&#39;np.square(number)&#39;, globals=globals(), setup=&#39;import numpy as np&#39;, number=1000000)<\/p>\n<p>print(f&#39;\u4e58\u6cd5\u8fd0\u7b97\u7b26\u8017\u65f6: {time1}&#39;)<\/p>\n<p>print(f&#39;pow\u51fd\u6570\u8017\u65f6: {time2}&#39;)<\/p>\n<p>print(f&#39;NumPy\u8017\u65f6: {time3}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e2a\u6027\u80fd\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u770b\u51fa\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u4e58\u6cd5\u8fd0\u7b97\u7b26\u662f\u6700\u5feb\u7684\u9009\u62e9\u3002\u800c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\uff0cNumPy\u7684\u6027\u80fd\u4f18\u52bf\u4f1a\u66f4\u52a0\u660e\u663e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\uff0c\u5305\u62ec\u4f7f\u7528\u4e58\u6cd5\u8fd0\u7b97\u7b26\u3001\u5185\u7f6e\u7684<code>pow<\/code>\u51fd\u6570\u3001\u4ee5\u53caNumPy\u5e93\u3002\u6839\u636e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002<strong>\u4e58\u6cd5\u8fd0\u7b97\u7b26\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u4e00\u822c\u7528\u9014\uff1b<code>pow<\/code>\u51fd\u6570\u901a\u7528\u6027\u5f3a\uff0c\u53ef\u4ee5\u8ba1\u7b97\u4efb\u610f\u6b21\u5e42\uff1bNumPy\u5e93\u9ad8\u6548\u7075\u6d3b\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6570\u503c\u8ba1\u7b97\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u65b9\u6cd5\u7684\u638c\u63e1\u548c\u7075\u6d3b\u5e94\u7528\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u7f16\u7a0b\u4efb\u52a1\u4e2d\u9ad8\u6548\u5730\u8fdb\u884c\u5e73\u65b9\u8ba1\u7b97\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728Python\u7f16\u7a0b\u4e2d\u8ba1\u7b97\u5e73\u65b9\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5feb\u901f\u8ba1\u7b97\u4e00\u4e2a\u6570\u5b57\u7684\u5e73\u65b9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba1\u7b97\u4e00\u4e2a\u6570\u5b57\u7684\u5e73\u65b9\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e58\u6cd5\u8fd0\u7b97\u7b26\uff0c\u6bd4\u5982 <code>number * number<\/code>\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26 <code>&lt;strong&gt;<\/code> \u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u65b9\u5f0f\uff0c\u4f8b\u5982 <code>number &lt;\/strong&gt; 2<\/code>\u3002\u5bf9\u4e8e\u9700\u8981\u8ba1\u7b97\u591a\u4e2a\u6570\u5e73\u65b9\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5185\u7f6e\u51fd\u6570 <code>map()<\/code>\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u65b9\uff1f<\/strong><br \/>\u867d\u7136Python\u6ca1\u6709\u4e13\u95e8\u7684\u5185\u7f6e\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u65b9\uff0c\u4f46\u53ef\u4ee5\u5229\u7528 <code>pow()<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c<code>pow(number, 2)<\/code> \u5c06\u8fd4\u56de\u6570\u5b57\u7684\u5e73\u65b9\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5728\u9700\u8981\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6307\u6570\u8fd0\u7b97\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5bf9\u4e00\u7ec4\u6570\u5b57\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5bf9\u4e00\u7ec4\u6570\u5b57\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u6570\u5b57\u5217\u8868 <code>numbers = [1, 2, 3, 4]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>[x &lt;strong&gt; 2 for x in numbers]<\/code> \u6765\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u4e2a\u6570\u5b57\u7684\u5e73\u65b9\u503c\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 <code>map()<\/code> \u51fd\u6570\uff0c\u7ed3\u5408 <code>lambda<\/code> \u8868\u8fbe\u5f0f\uff0c\u4f8b\u5982 <code>list(map(lambda x: x &lt;\/strong&gt; 2, numbers))<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5e73\u65b9\u4e00\u4e2a\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u4e58\u6cd5\u8fd0\u7b97\u7b26\u3001\u5185\u7f6e\u7684pow\u51fd\u6570\u3001\u4ee5\u53caNumPy\u5e93\u4e2d\u7684squ [&hellip;]","protected":false},"author":3,"featured_media":1060580,"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\/1060571"}],"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=1060571"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060571\/revisions"}],"predecessor-version":[{"id":1060581,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060571\/revisions\/1060581"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1060580"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1060571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1060571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1060571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}