{"id":1100328,"date":"2025-01-08T15:38:56","date_gmt":"2025-01-08T07:38:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100328.html"},"modified":"2025-01-08T15:38:58","modified_gmt":"2025-01-08T07:38:58","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e6%95%b0%e7%bb%84%e4%b8%ad%e6%8f%90%e5%8f%96%e5%87%a0%e4%b8%aa-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100328.html","title":{"rendered":"python\u5982\u4f55\u5728\u6570\u7ec4\u4e2d\u63d0\u53d6\u51e0\u4e2a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063624\/a10c3dcc-52a4-4dd4-ba84-fcf7547713da.webp\" alt=\"python\u5982\u4f55\u5728\u6570\u7ec4\u4e2d\u63d0\u53d6\u51e0\u4e2a\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u4ece\u6570\u7ec4\u4e2d\u63d0\u53d6\u5143\u7d20\uff0c\u5305\u62ec\u7d22\u5f15\u3001\u5207\u7247\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u4ee5\u53ca\u4f7f\u7528numpy\u5e93\u4e2d\u7684\u9ad8\u7ea7\u7d22\u5f15\u548c\u5e03\u5c14\u7d22\u5f15\u3002<\/strong> \u5176\u4e2d\uff0c\u7d22\u5f15\u548c\u5207\u7247\u662f\u6700\u5e38\u7528\u7684\u57fa\u672c\u65b9\u6cd5\u3002\u7d22\u5f15\u7528\u4e8e\u63d0\u53d6\u5355\u4e2a\u5143\u7d20\uff0c\u800c\u5207\u7247\u7528\u4e8e\u63d0\u53d6\u591a\u4e2a\u8fde\u7eed\u7684\u5143\u7d20\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\u6765\u5e2e\u52a9\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6570\u7ec4\u901a\u5e38\u662f\u6307\u5217\u8868\uff08list\uff09\u6216\u4f7f\u7528numpy\u5e93\u7684\u6570\u7ec4\u5bf9\u8c61\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u7d22\u5f15\u90fd\u662f\u6700\u57fa\u672c\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u7d22\u5f15\u662f\u901a\u8fc7\u65b9\u62ec\u53f7\u6765\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4f7f\u7528\u7d22\u5f15\u63d0\u53d6\u5143\u7d20<\/p>\n<p>array = [10, 20, 30, 40, 50]<\/p>\n<p>element = array[2]  # \u63d0\u53d6\u7b2c3\u4e2a\u5143\u7d20\uff08\u7d22\u5f15\u4ece0\u5f00\u59cb\uff09<\/p>\n<p>print(element)  # \u8f93\u51fa\uff1a30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e94\u4e2a\u6574\u6570\u7684\u5217\u8868\uff0c\u5e76\u4f7f\u7528\u7d22\u5f15\u6765\u63d0\u53d6\u7b2c\u4e09\u4e2a\u5143\u7d20\uff08\u7d22\u5f15\u4ece0\u5f00\u59cb\uff09\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5207\u7247<\/p>\n<\/p>\n<p><p>\u5207\u7247\u662f\u4e00\u79cd\u66f4\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u63d0\u53d6\u6570\u7ec4\u4e2d\u7684\u591a\u4e2a\u8fde\u7eed\u5143\u7d20\u3002\u5207\u7247\u8bed\u6cd5\u4e3a<code>array[start:end:step]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff0c<code>end<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff0c<code>step<\/code>\u662f\u6b65\u957f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4f7f\u7528\u5207\u7247\u63d0\u53d6\u5143\u7d20<\/p>\n<p>array = [10, 20, 30, 40, 50]<\/p>\n<p>sub_array = array[1:4]  # \u63d0\u53d6\u7b2c2\u52304\u4e2a\u5143\u7d20\uff08\u7d22\u5f15\u4ece0\u5f00\u59cb\uff09<\/p>\n<p>print(sub_array)  # \u8f93\u51fa\uff1a[20, 30, 40]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u8bed\u6cd5\u63d0\u53d6\u4e86\u7b2c\u4e8c\u5230\u7b2c\u56db\u4e2a\u5143\u7d20\uff0c\u6ce8\u610f\u7ed3\u675f\u7d22\u5f15\u4e0d\u5305\u62ec\u5728\u5185\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u8bed\u6cd5\uff0c\u7528\u4e8e\u4ece\u73b0\u6709\u5217\u8868\u4e2d\u63d0\u53d6\u6216\u53d8\u6362\u5143\u7d20\u3002\u5b83\u53ef\u4ee5\u4e0e\u6761\u4ef6\u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u63d0\u53d6\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u53d6\u5143\u7d20<\/p>\n<p>array = [10, 20, 30, 40, 50]<\/p>\n<p>even_elements = [x for x in array if x % 2 == 0]  # \u63d0\u53d6\u6240\u6709\u5076\u6570\u5143\u7d20<\/p>\n<p>print(even_elements)  # \u8f93\u51fa\uff1a[10, 20, 30, 40, 50]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u53d6\u4e86\u6240\u6709\u5076\u6570\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001numpy\u5e93\u4e2d\u7684\u9ad8\u7ea7\u7d22\u5f15\u548c\u5e03\u5c14\u7d22\u5f15<\/p>\n<\/p>\n<p><p>numpy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u7d22\u5f15\u548c\u5e03\u5c14\u7d22\u5f15\u529f\u80fd\uff0c\u4f7f\u5f97\u5728\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u4e2d\u63d0\u53d6\u5143\u7d20\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u7075\u6d3b\u3002numpy\u6570\u7ec4\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u64cd\u4f5c\uff0c\u8fd9\u4f7f\u5f97\u5b83\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u6d41\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u4f7f\u7528numpy\u9ad8\u7ea7\u7d22\u5f15<\/strong><\/h2>\n<p>array = np.array([10, 20, 30, 40, 50])<\/p>\n<p>indices = [1, 3, 4]<\/p>\n<p>elements = array[indices]  # \u63d0\u53d6\u7b2c2\u30014\u548c5\u4e2a\u5143\u7d20<\/p>\n<p>print(elements)  # \u8f93\u51fa\uff1a[20, 40, 50]<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u4f7f\u7528numpy\u5e03\u5c14\u7d22\u5f15<\/strong><\/h2>\n<p>array = np.array([10, 20, 30, 40, 50])<\/p>\n<p>bool_array = array &gt; 25  # \u521b\u5efa\u5e03\u5c14\u6570\u7ec4<\/p>\n<p>filtered_elements = array[bool_array]  # \u63d0\u53d6\u5927\u4e8e25\u7684\u5143\u7d20<\/p>\n<p>print(filtered_elements)  # \u8f93\u51fa\uff1a[30, 40, 50]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e24\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528numpy\u7684\u9ad8\u7ea7\u7d22\u5f15\u548c\u5e03\u5c14\u7d22\u5f15\u6765\u63d0\u53d6\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b9e\u4f8b\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u7ed3\u5408\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u7f16\u5199\u4e00\u4e2a\u7efc\u5408\u5b9e\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4ece\u6570\u7ec4\u4e2d\u63d0\u53d6\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b100\u4e2a\u968f\u673a\u6574\u6570\u7684numpy\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.random.randint(0, 100, size=100)<\/p>\n<h2><strong>\u63d0\u53d6\u524d10\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>first_10_elements = array[:10]<\/p>\n<p>print(&quot;\u524d10\u4e2a\u5143\u7d20:&quot;, first_10_elements)<\/p>\n<h2><strong>\u63d0\u53d6\u6700\u540e10\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>last_10_elements = array[-10:]<\/p>\n<p>print(&quot;\u6700\u540e10\u4e2a\u5143\u7d20:&quot;, last_10_elements)<\/p>\n<h2><strong>\u63d0\u53d6\u7d22\u5f15\u4e3a\u5947\u6570\u7684\u5143\u7d20<\/strong><\/h2>\n<p>odd_index_elements = array[1::2]<\/p>\n<p>print(&quot;\u7d22\u5f15\u4e3a\u5947\u6570\u7684\u5143\u7d20:&quot;, odd_index_elements)<\/p>\n<h2><strong>\u63d0\u53d6\u6240\u6709\u5927\u4e8e50\u7684\u5143\u7d20<\/strong><\/h2>\n<p>greater_than_50_elements = array[array &gt; 50]<\/p>\n<p>print(&quot;\u5927\u4e8e50\u7684\u5143\u7d20:&quot;, greater_than_50_elements)<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c10\u300120\u300130\u300140\u300150\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>specific_indices = [9, 19, 29, 39, 49]<\/p>\n<p>specific_elements = array[specific_indices]<\/p>\n<p>print(&quot;\u7b2c10\u300120\u300130\u300140\u300150\u4e2a\u5143\u7d20:&quot;, specific_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7efc\u5408\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b100\u4e2a\u968f\u673a\u6574\u6570\u7684numpy\u6570\u7ec4\uff0c\u5e76\u5c55\u793a\u4e86\u5982\u4f55\u63d0\u53d6\u524d10\u4e2a\u5143\u7d20\u3001\u6700\u540e10\u4e2a\u5143\u7d20\u3001\u7d22\u5f15\u4e3a\u5947\u6570\u7684\u5143\u7d20\u3001\u6240\u6709\u5927\u4e8e50\u7684\u5143\u7d20\u4ee5\u53ca\u7279\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u4ece\u6570\u7ec4\u4e2d\u63d0\u53d6\u5143\u7d20\u3002\u65e0\u8bba\u662f\u5904\u7406\u7b80\u5355\u7684\u5217\u8868\u8fd8\u662f\u4f7f\u7528numpy\u8fdb\u884c\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u591f\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u5728Python\u4e2d\u5904\u7406\u6570\u7ec4\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4ece\u6570\u7ec4\u4e2d\u63d0\u53d6\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u3001\u7d22\u5f15\u6216\u6761\u4ef6\u8fc7\u6ee4\u6765\u63d0\u53d6\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4, 5]<\/code>\uff0c\u8981\u63d0\u53d6\u7b2c\u4e8c\u5230\u7b2c\u56db\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247<code>my_list[1:4]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>[2, 3, 4]<\/code>\u3002\u5bf9\u4e8e\u6761\u4ef6\u8fc7\u6ee4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f8b\u5982\u63d0\u53d6\u5927\u4e8e3\u7684\u6240\u6709\u5143\u7d20\uff1a<code>[x for x in my_list if x &gt; 3]<\/code>\uff0c\u7ed3\u679c\u4e3a<code>[4, 5]<\/code>\u3002<\/p>\n<p><strong>\u4f7f\u7528NumPy\u5e93\u5982\u4f55\u9ad8\u6548\u63d0\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u53ef\u4ee5\u9ad8\u6548\u5904\u7406\u6570\u7ec4\u3002\u5982\u679c\u60a8\u4f7f\u7528NumPy\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u8f7b\u677e\u63d0\u53d6\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9aNumPy\u6570\u7ec4<code>import numpy as np; arr = np.array([1, 2, 3, 4, 5])<\/code>\uff0c\u8981\u63d0\u53d6\u5927\u4e8e3\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>arr[arr &gt; 3]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>array([4, 5])<\/code>\u3002NumPy\u7684\u5207\u7247\u548c\u7d22\u5f15\u64cd\u4f5c\u4e0ePython\u5185\u7f6e\u5217\u8868\u7c7b\u4f3c\uff0c\u4f46\u5728\u6027\u80fd\u4e0a\u66f4\u4e3a\u4f18\u8d8a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4ece\u6570\u7ec4\u4e2d\u63d0\u53d6\u591a\u4e2a\u4e0d\u8fde\u7eed\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u63d0\u53d6\u4e0d\u8fde\u7eed\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u5217\u8868\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u5217\u8868<code>my_list = [10, 20, 30, 40, 50]<\/code>\uff0c\u5982\u679c\u60f3\u63d0\u53d6\u7b2c1\u548c\u7b2c3\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff1a<code>[my_list[i] for i in [0, 2]]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>[10, 30]<\/code>\u3002\u5728NumPy\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\uff1a<code>arr[[0, 2]]<\/code>\uff0c\u540c\u6837\u53ef\u4ee5\u83b7\u5f97\u9700\u8981\u7684\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u4ece\u6570\u7ec4\u4e2d\u63d0\u53d6\u5143\u7d20\uff0c\u5305\u62ec\u7d22\u5f15\u3001\u5207\u7247\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u4ee5\u53ca\u4f7f\u7528numpy\u5e93\u4e2d\u7684\u9ad8\u7ea7\u7d22\u5f15 [&hellip;]","protected":false},"author":3,"featured_media":1100336,"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\/1100328"}],"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=1100328"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100328\/revisions"}],"predecessor-version":[{"id":1100339,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100328\/revisions\/1100339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100336"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}