{"id":1064997,"date":"2024-12-31T16:13:52","date_gmt":"2024-12-31T08:13:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1064997.html"},"modified":"2024-12-31T16:13:55","modified_gmt":"2024-12-31T08:13:55","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e6%95%b0%e7%bb%84%e5%85%83%e7%b4%a0%e5%b0%b1%e8%a1%8c%e8%bf%90%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1064997.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u6570\u7ec4\u5143\u7d20\u5c31\u884c\u8fd0\u7b97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c471f0fd-bfbb-405f-b094-b758959feb75.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5bf9\u6570\u7ec4\u5143\u7d20\u5c31\u884c\u8fd0\u7b97\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5185\u7f6e\u7684map\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u3002<\/strong> \u5176\u4e2d\u6700\u5e38\u7528\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528NumPy\u5e93\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\uff0c\u5e76\u89e3\u91ca\u5176\u4f18\u7f3a\u70b9\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\uff08List Comprehension\uff09\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u5217\u8868\u3002\u901a\u8fc7\u5217\u8868\u89e3\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6570\u7ec4\uff08\u5217\u8868\uff09\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/p>\n<p>array = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97<\/strong><\/h2>\n<p>squared_array = [x2 for x in array]<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5217\u8868\u89e3\u6790 <code>[x2 for x in array]<\/code>\uff0c\u6211\u4eec\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u4e86\u5e73\u65b9\u8fd0\u7b97\uff0c\u5e76\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4 <code>squared_array<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ol>\n<li><strong>\u7b80\u6d01<\/strong>\uff1a\u4ee3\u7801\u91cf\u5c11\uff0c\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u76f8\u6bd4\u4e8e\u4f20\u7edf\u7684\u5faa\u73af\u904d\u5386\uff0c\u5217\u8868\u89e3\u6790\u5177\u6709\u66f4\u597d\u7684\u6027\u80fd\u3002<\/li>\n<\/ol>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ol>\n<li><strong>\u5185\u5b58\u5360\u7528<\/strong>\uff1a\u5bf9\u4e8e\u7279\u522b\u5927\u7684\u6570\u7ec4\uff0c\u5217\u8868\u89e3\u6790\u53ef\u80fd\u4f1a\u5360\u7528\u8f83\u591a\u7684\u5185\u5b58\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u5217\u8868\u89e3\u6790\u9002\u7528\u4e8e\u7b80\u5355\u7684\u64cd\u4f5c\uff0c\u5bf9\u4e8e\u590d\u6742\u7684\u8fd0\u7b97\u903b\u8f91\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u6700\u91cd\u8981\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u4f7f\u7528NumPy\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u9ad8\u6548\u7684\u6570\u7ec4\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97<\/strong><\/h2>\n<p>squared_array = np.square(array)<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86NumPy\u5e93\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2aNumPy\u6570\u7ec4 <code>array<\/code>\u3002\u7136\u540e\uff0c\u4f7f\u7528 <code>np.square<\/code> \u51fd\u6570\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u4e86\u5e73\u65b9\u8fd0\u7b97\uff0c\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4 <code>squared_array<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ol>\n<li><strong>\u9ad8\u6548<\/strong>\uff1aNumPy\u5e95\u5c42\u4f7f\u7528C\u8bed\u8a00\u5b9e\u73b0\uff0c\u8ba1\u7b97\u901f\u5ea6\u975e\u5e38\u5feb\u3002<\/li>\n<li><strong>\u529f\u80fd\u4e30\u5bcc<\/strong>\uff1a\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\u548c\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u5185\u5b58\u7ba1\u7406<\/strong>\uff1aNumPy\u6570\u7ec4\u5728\u5185\u5b58\u7ba1\u7406\u65b9\u9762\u66f4\u52a0\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5e93<\/strong>\uff1a\u9700\u8981\u989d\u5916\u5b89\u88c5NumPy\u5e93\u3002<\/li>\n<li><strong>\u5b66\u4e60\u6210\u672c<\/strong>\uff1a\u5bf9\u4e8e\u65b0\u624b\u6765\u8bf4\uff0c\u5b66\u4e60\u548c\u638c\u63e1NumPy\u9700\u8981\u4e00\u5b9a\u7684\u65f6\u95f4\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5185\u7f6e\u7684map\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684 <code>map<\/code> \u51fd\u6570\u7528\u4e8e\u5c06\u6307\u5b9a\u7684\u51fd\u6570\u5e94\u7528\u4e8e\u7ed9\u5b9a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\uff09\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/p>\n<p>array = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528map\u51fd\u6570\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97<\/strong><\/h2>\n<p>squared_array = list(map(lambda x: x2, array))<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>map<\/code> \u51fd\u6570\u548c <code>lambda<\/code> \u8868\u8fbe\u5f0f\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u4e86\u5e73\u65b9\u8fd0\u7b97\uff0c\u5e76\u901a\u8fc7 <code>list<\/code> \u51fd\u6570\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ol>\n<li><strong>\u7b80\u6d01<\/strong>\uff1a\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<li><strong>\u7075\u6d3b<\/strong>\uff1a\u53ef\u4ee5\u65b9\u4fbf\u5730\u5e94\u7528\u4efb\u610f\u51fd\u6570\u3002<\/li>\n<\/ol>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ol>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u76f8\u6bd4\u4e8eNumPy\uff0c\u6027\u80fd\u7a0d\u900a\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u8f83\u5dee\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4f20\u7edf\u7684for\u5faa\u73af\u904d\u5386\u6570\u7ec4\u5e76\u5bf9\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\u662f\u6700\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/p>\n<p>array = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97<\/strong><\/h2>\n<p>squared_array = []<\/p>\n<p>for x in array:<\/p>\n<p>    squared_array.append(x2)<\/p>\n<p>print(squared_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u89e3\u91ca\uff1a<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u6700\u7ec8\u5c06\u7ed3\u679c\u6dfb\u52a0\u5230\u65b0\u7684\u6570\u7ec4 <code>squared_array<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u70b9\uff1a<\/h4>\n<\/p>\n<ol>\n<li><strong>\u7075\u6d3b<\/strong>\uff1a\u9002\u7528\u4e8e\u5404\u79cd\u590d\u6742\u7684\u8fd0\u7b97\u903b\u8f91\u3002<\/li>\n<li><strong>\u6613\u7406\u89e3<\/strong>\uff1a\u4ee3\u7801\u903b\u8f91\u7b80\u5355\u660e\u4e86\uff0c\u9002\u5408\u65b0\u624b\u3002<\/li>\n<\/ol>\n<p><h4>\u7f3a\u70b9\uff1a<\/h4>\n<\/p>\n<ol>\n<li><strong>\u4ee3\u7801\u91cf\u5927<\/strong>\uff1a\u76f8\u6bd4\u4e8e\u5176\u4ed6\u65b9\u6cd5\uff0c\u4ee3\u7801\u8f83\u4e3a\u5197\u957f\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u6027\u80fd\u8f83\u5dee\u3002<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u3002<strong>\u5217\u8868\u89e3\u6790<\/strong>\u9002\u7528\u4e8e\u7b80\u5355\u7684\u64cd\u4f5c\uff0c<strong>NumPy<\/strong>\u9002\u7528\u4e8e\u9ad8\u6548\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c<strong>map\u51fd\u6570<\/strong>\u9002\u7528\u4e8e\u51fd\u6570\u5e94\u7528\u573a\u666f\uff0c<strong>\u5faa\u73af\u904d\u5386<\/strong>\u9002\u7528\u4e8e\u590d\u6742\u903b\u8f91\u8fd0\u7b97\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1. \u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u5404\u79cd\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u5bf9\u4f20\u611f\u5668\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3001\u5bf9\u56fe\u50cf\u50cf\u7d20\u8fdb\u884c\u64cd\u4f5c\u7b49\u3002NumPy\u5e93\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\u8868\u73b0\u5c24\u4e3a\u51fa\u8272\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6a21\u62df\u7684\u4f20\u611f\u5668\u6570\u636e\u6570\u7ec4<\/strong><\/h2>\n<p>sensor_data = np.array([23.4, 25.1, 24.8, 22.9, 23.5])<\/p>\n<h2><strong>\u5bf9\u6570\u636e\u8fdb\u884c\u5f52\u4e00\u5316\u5904\u7406<\/strong><\/h2>\n<p>normalized_data = (sensor_data - np.min(sensor_data)) \/ (np.max(sensor_data) - np.min(sensor_data))<\/p>\n<p>print(normalized_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u79d1\u5b66\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u9886\u57df\uff0c\u5e38\u5e38\u9700\u8981\u5bf9\u5927\u89c4\u6a21\u6570\u7ec4\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3001\u7edf\u8ba1\u5206\u6790\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix_a = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix_b = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u77e9\u9635\u76f8\u4e58<\/strong><\/h2>\n<p>matrix_product = np.dot(matrix_a, matrix_b)<\/p>\n<p>print(matrix_product)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \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\u4e8c\u7ef4\u6216\u4e09\u7ef4\u6570\u7ec4\u3002\u5bf9\u56fe\u50cf\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff08\u5982\u6ee4\u6ce2\u3001\u53d8\u6362\u3001\u589e\u5f3a\u7b49\uff09\u65f6\uff0c\u9700\u8981\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\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\u5e76\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>image = Image.open(&#39;example.jpg&#39;)<\/p>\n<p>image_array = np.array(image)<\/p>\n<h2><strong>\u5bf9\u56fe\u50cf\u8fdb\u884c\u7070\u5ea6\u5904\u7406<\/strong><\/h2>\n<p>gray_image_array = np.dot(image_array[..., :3], [0.2989, 0.5870, 0.1140])<\/p>\n<h2><strong>\u8f6c\u6362\u56de\u56fe\u50cf\u5e76\u4fdd\u5b58<\/strong><\/h2>\n<p>gray_image = Image.fromarray(gray_image_array.astype(&#39;uint8&#39;))<\/p>\n<p>gray_image.save(&#39;gray_example.jpg&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u793a\u4f8b\u5c55\u793a\u4e86\u5728\u6570\u636e\u5904\u7406\u3001\u79d1\u5b66\u8ba1\u7b97\u548c\u56fe\u50cf\u5904\u7406\u7b49\u5b9e\u9645\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u5982\u4f55\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u5404\u79cd\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u5982\u4f55\u5bf9\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u6216\u8005\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5bf9\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5e73\u65b9\u8fd0\u7b97\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">arr = [1, 2, 3, 4]\nsquared = list(map(lambda x: x ** 2, arr))\n# \u6216\u8005\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\nsquared = [x ** 2 for x in arr]\n<\/code><\/pre>\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5305\u542b\u6bcf\u4e2a\u5143\u7d20\u7684\u5e73\u65b9\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u5bf9\u6570\u7ec4\u8fdb\u884c\u9ad8\u6548\u8fd0\u7b97\uff1f<\/strong><br \/>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u3002\u5982\u679c\u8981\u5bf9\u6570\u7ec4\u6267\u884c\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u7b49\u8fd0\u7b97\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528NumPy\u7684\u6570\u7ec4\u8fd0\u7b97\u529f\u80fd\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\narr = np.array([1, 2, 3, 4])\nresult = arr * 2  # \u6bcf\u4e2a\u5143\u7d20\u90fd\u4e58\u4ee52\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6267\u884c\u901f\u5ea6\u66f4\u5feb\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u5143\u7d20\u7684\u8fd0\u7b97\uff1f<\/strong><br \/>\u9488\u5bf9\u591a\u7ef4\u6570\u7ec4\uff0cNumPy\u540c\u6837\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\u3002\u53ef\u4ee5\u901a\u8fc7\u591a\u7ef4\u6570\u7ec4\u7684\u5e7f\u64ad\u673a\u5236\uff0c\u8f7b\u677e\u5730\u8fdb\u884c\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u5e0c\u671b\u5bf9\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e0a\u4e00\u4e2a\u6807\u91cf\u503c\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\narr = np.array([[1, 2], [3, 4]])\nresult = arr + 10  # \u6240\u6709\u5143\u7d20\u90fd\u52a0\u4e0a10\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\uff0c\u5e76\u4e14\u53ef\u4ee5\u8fdb\u884c\u590d\u6742\u7684\u77e9\u9635\u8fd0\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u8fd0\u7b97\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5185\u7f6e\u7684map\u51fd\u6570\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1065010,"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\/1064997"}],"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=1064997"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064997\/revisions"}],"predecessor-version":[{"id":1065012,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064997\/revisions\/1065012"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1065010"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1064997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1064997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1064997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}