{"id":1141857,"date":"2025-01-08T22:34:44","date_gmt":"2025-01-08T14:34:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141857.html"},"modified":"2025-01-08T22:34:47","modified_gmt":"2025-01-08T14:34:47","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e7%bb%84%e5%90%8c%e6%97%b6%e5%87%8f%e4%b8%80%e4%b8%aa%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141857.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6570\u7ec4\u540c\u65f6\u51cf\u4e00\u4e2a\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104444\/d3cee8d2-28d7-4b84-aac7-9a2b62eae63f.webp\" alt=\"python\u5982\u4f55\u5c06\u6570\u7ec4\u540c\u65f6\u51cf\u4e00\u4e2a\u6570\u5b57\" \/><\/p>\n<p><p> <strong>Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001NumPy\u5e93\u6216\u5faa\u73af\u6765\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u540c\u65f6\u51cf\u53bb\u4e00\u4e2a\u6570\u5b57\u3002\u5217\u8868\u89e3\u6790\u6700\u7b80\u6d01\u3001NumPy\u6027\u80fd\u6700\u597d\u3001\u5faa\u73af\u6700\u76f4\u89c2\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5177\u4f53\u6765\u8bf4\uff0c\u4f7f\u7528\u5217\u8868\u89e3\u6790\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u53ef\u8bfb\u6027\uff0c\u4ee5\u4e0b\u662f\u8be6\u7ec6\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [10, 20, 30, 40]<\/p>\n<p>number_to_subtract = 5<\/p>\n<p>result = [x - number_to_subtract for x in array]<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5217\u8868\u89e3\u6790\u901a\u8fc7\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u51cf\u53bb\u6307\u5b9a\u7684\u6570\u5b57\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002 <\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\uff0c\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u548c\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662fPython\u4e2d\u7684\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u5b83\u80fd\u591f\u51cf\u5c11\u4ee3\u7801\u884c\u6570\uff0c\u5e76\u4e14\u901a\u5e38\u6bd4\u4f20\u7edf\u7684for\u5faa\u73af\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_list = [expression for item in iterable]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u53ef\u4ee5\u5c06\u5176\u7528\u4e8e\u51cf\u53bb\u4e00\u4e2a\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [10, 20, 30, 40]<\/p>\n<p>number_to_subtract = 5<\/p>\n<p>result = [x - number_to_subtract for x in array]<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa <code>[5, 15, 25, 35]<\/code>\u3002\u5b83\u5728\u904d\u5386<code>array<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u65f6\uff0c\u51cf\u53bb\u4e86<code>number_to_subtract<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u590d\u6742\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u4e0d\u4ec5\u9650\u4e8e\u7b80\u5355\u7684\u51cf\u6cd5\u64cd\u4f5c\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [10, 20, 30, 40]<\/p>\n<p>number_to_subtract = 5<\/p>\n<p>result = [x - number_to_subtract if x &gt; 15 else x for x in array]<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa <code>[10, 15, 25, 35]<\/code>\uff0c\u53ea\u6709\u5927\u4e8e15\u7684\u5143\u7d20\u624d\u4f1a\u88ab\u51cf\u53bb\u6307\u5b9a\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u4e00\u4e2a\u5f3a\u5927\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\u548c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u975e\u5e38\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5NumPy<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\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>2\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5bf9\u6574\u4e2a\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\uff0c\u800c\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([10, 20, 30, 40])<\/p>\n<p>number_to_subtract = 5<\/p>\n<p>result = array - number_to_subtract<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa <code>[ 5, 15, 25, 35]<\/code>\u3002NumPy\u5185\u90e8\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u4f7f\u5f97\u8fd9\u79cd\u64cd\u4f5c\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u66f4\u591a\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>NumPy\u4e0d\u4ec5\u9650\u4e8e\u7b80\u5355\u7684\u51cf\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u5176\u4ed6\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u9009\u62e9\u6027\u5730\u5bf9\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([10, 20, 30, 40])<\/p>\n<p>number_to_subtract = 5<\/p>\n<p>result = np.where(array &gt; 15, array - number_to_subtract, array)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa <code>[10, 15, 25, 35]<\/code>\uff0c\u53ea\u6709\u5927\u4e8e15\u7684\u5143\u7d20\u624d\u4f1a\u88ab\u51cf\u53bb\u6307\u5b9a\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5217\u8868\u89e3\u6790\u548cNumPy\u90fd\u63d0\u4f9b\u4e86\u7b80\u6d01\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5faa\u73af\u53ef\u80fd\u4f1a\u66f4\u52a0\u76f4\u89c2\uff0c\u7279\u522b\u662f\u5f53\u9700\u8981\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u53ef\u4ee5\u9010\u4e00\u5904\u7406\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [10, 20, 30, 40]<\/p>\n<p>number_to_subtract = 5<\/p>\n<p>result = []<\/p>\n<p>for x in array:<\/p>\n<p>    result.append(x - number_to_subtract)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa <code>[5, 15, 25, 35]<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u590d\u6742\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5faa\u73af\u4e5f\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u6761\u4ef6\u5224\u65ad\u548c\u591a\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [10, 20, 30, 40]<\/p>\n<p>number_to_subtract = 5<\/p>\n<p>result = []<\/p>\n<p>for x in array:<\/p>\n<p>    if x &gt; 15:<\/p>\n<p>        result.append(x - number_to_subtract)<\/p>\n<p>    else:<\/p>\n<p>        result.append(x)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa <code>[10, 15, 25, 35]<\/code>\uff0c\u53ea\u6709\u5927\u4e8e15\u7684\u5143\u7d20\u624d\u4f1a\u88ab\u51cf\u53bb\u6307\u5b9a\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u7684\u91cd\u8981\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u5bf9\u5217\u8868\u89e3\u6790\u3001NumPy\u548c\u5faa\u73af\u4e09\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5217\u8868\u89e3\u6790 vs \u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u901a\u5e38\u6bd4\u4f20\u7edf\u7684for\u5faa\u73af\u66f4\u9ad8\u6548\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u8f83\u5c0f\u7684\u6570\u7ec4\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6027\u80fd\u6d4b\u8bd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>array = list(range(1000000))<\/p>\n<p>number_to_subtract = 5<\/p>\n<h2><strong>\u5217\u8868\u89e3\u6790<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>result = [x - number_to_subtract for x in array]<\/p>\n<p>print(&quot;\u5217\u8868\u89e3\u6790\u65f6\u95f4\uff1a&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u5faa\u73af<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>result = []<\/p>\n<p>for x in array:<\/p>\n<p>    result.append(x - number_to_subtract)<\/p>\n<p>print(&quot;\u5faa\u73af\u65f6\u95f4\uff1a&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5217\u8868\u89e3\u6790\u901a\u5e38\u4f1a\u6bd4\u5faa\u73af\u66f4\u5feb\u3002<\/p>\n<\/p>\n<p><h4>2\u3001NumPy\u7684\u6027\u80fd\u4f18\u52bf<\/h4>\n<\/p>\n<p><p>NumPy\u5728\u5904\u7406\u5927\u578b\u6570\u7ec4\u65f6\u5177\u6709\u663e\u8457\u7684\u6027\u80fd\u4f18\u52bf\uff0c\u56e0\u4e3a\u5b83\u662f\u7528C\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u5e76\u4e14\u8fdb\u884c\u4e86\u9ad8\u5ea6\u4f18\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6027\u80fd\u6d4b\u8bd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import time<\/p>\n<p>array = np.arange(1000000)<\/p>\n<p>number_to_subtract = 5<\/p>\n<h2><strong>NumPy<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>result = array - number_to_subtract<\/p>\n<p>print(&quot;NumPy\u65f6\u95f4\uff1a&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cNumPy\u7684\u6027\u80fd\u901a\u5e38\u4f1a\u663e\u8457\u4f18\u4e8e\u5217\u8868\u89e3\u6790\u548c\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0cNumPy\u662f\u9996\u9009\uff0c\u56e0\u4e3a\u5b83\u7684\u6027\u80fd\u548c\u529f\u80fd\u90fd\u975e\u5e38\u5f3a\u5927\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u65f6\uff0cNumPy\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u8ba1\u7b97\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7b80\u5355\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u7279\u522b\u662f\u5f53\u6570\u7ec4\u8f83\u5c0f\u65f6\uff0c\u5217\u8868\u89e3\u6790\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u548c\u9ad8\u6548\u7684\u9009\u62e9\u3002\u5b83\u7684\u8bed\u6cd5\u7b80\u6d01\uff0c\u53ef\u8bfb\u6027\u9ad8\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u590d\u6742\u903b\u8f91<\/h4>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u903b\u8f91\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528\u5faa\u73af\u53ef\u80fd\u4f1a\u66f4\u52a0\u76f4\u89c2\u548c\u7075\u6d3b\u3002\u7279\u522b\u662f\u5f53\u64cd\u4f5c\u9700\u8981\u591a\u4e2a\u6b65\u9aa4\u6216\u6761\u4ef6\u5224\u65ad\u65f6\uff0c\u5faa\u73af\u80fd\u591f\u66f4\u6e05\u6670\u5730\u8868\u8fbe\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001NumPy\u5e93\u6216\u5faa\u73af\u6765\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u540c\u65f6\u51cf\u53bb\u4e00\u4e2a\u6570\u5b57\u3002<\/strong> \u5217\u8868\u89e3\u6790\u6700\u7b80\u6d01\u3001NumPy\u6027\u80fd\u6700\u597d\u3001\u5faa\u73af\u6700\u76f4\u89c2\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u8003\u8651\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u6027\u80fd\u548c\u7075\u6d3b\u6027\u3002\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0cNumPy\u65e0\u7591\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u800c\u5728\u5904\u7406\u8f83\u5c0f\u7684\u6570\u636e\u96c6\u6216\u7b80\u5355\u7684\u64cd\u4f5c\u65f6\uff0c\u5217\u8868\u89e3\u6790\u548c\u5faa\u73af\u90fd\u53ef\u4ee5\u63d0\u4f9b\u826f\u597d\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u540c\u65f6\u51cf\u53bb\u4e00\u4e2a\u7279\u5b9a\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u9996\u5148\uff0c\u901a\u8fc7NumPy\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u7136\u540e\u5c06\u8981\u51cf\u53bb\u7684\u6570\u5b57\u4e0e\u6570\u7ec4\u8fdb\u884c\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>array - number<\/code>\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u540c\u65f6\u51cf\u53bb\u6307\u5b9a\u7684\u6570\u5b57\u3002\u8fd9\u6837\u505a\u7684\u4f18\u52bf\u5728\u4e8e\u4ee3\u7801\u7b80\u6d01\u4e14\u8ba1\u7b97\u6548\u7387\u9ad8\u3002<\/p>\n<p><strong>\u4f7f\u7528\u539f\u751fPython\u5b9e\u73b0\u6570\u7ec4\u51cf\u6cd5\u7684\u65b9\u5f0f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528NumPy\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u6570\u7ec4\u7684\u5143\u7d20\u51cf\u6cd5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>[x - number for x in array]<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u51cf\u53bb\u4e86\u6307\u5b9a\u7684\u6570\u5b57\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u5904\u7406\uff0c\u4f46\u5728\u6027\u80fd\u4e0a\u53ef\u80fd\u4e0d\u5982NumPy\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u51cf\u6cd5\u8fd0\u7b97\u540e\u53ef\u80fd\u51fa\u73b0\u7684\u8d1f\u6570\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6570\u7ec4\u51cf\u6cd5\u8fd0\u7b97\u65f6\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u8d1f\u6570\u3002\u5982\u679c\u5e0c\u671b\u907f\u514d\u8d1f\u6570\u7684\u51fa\u73b0\uff0c\u53ef\u4ee5\u5728\u51cf\u6cd5\u524d\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u786e\u4fdd\u6bcf\u4e2a\u5143\u7d20\u5728\u51cf\u53bb\u6570\u5b57\u540e\u4ecd\u5927\u4e8e\u6216\u7b49\u4e8e\u96f6\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7ed3\u679c\u7b26\u5408\u9700\u6c42\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u51cf\u53bb\u4e00\u4e2a\u52a8\u6001\u53d8\u5316\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5c06\u5f85\u51cf\u53bb\u7684\u6570\u5b57\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u53d8\u91cf\uff0c\u5e76\u5728\u6570\u7ec4\u8fd0\u7b97\u65f6\u4f7f\u7528\u8be5\u53d8\u91cf\u3002\u53ea\u9700\u5728\u51cf\u6cd5\u8fd0\u7b97\u4e2d\u5f15\u7528\u8fd9\u4e2a\u53d8\u91cf\uff0c\u5c31\u80fd\u5b9e\u73b0\u52a8\u6001\u53d8\u5316\u7684\u6548\u679c\u3002\u8fd9\u6837\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u4e1a\u52a1\u9700\u6c42\u7075\u6d3b\u8c03\u6574\u51cf\u53bb\u7684\u6570\u5b57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001NumPy\u5e93\u6216\u5faa\u73af\u6765\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u540c\u65f6\u51cf\u53bb\u4e00\u4e2a\u6570\u5b57\u3002\u5217\u8868\u89e3\u6790\u6700\u7b80\u6d01\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1141871,"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\/1141857"}],"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=1141857"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141857\/revisions"}],"predecessor-version":[{"id":1141874,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141857\/revisions\/1141874"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141871"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}