{"id":1076596,"date":"2025-01-08T11:54:49","date_gmt":"2025-01-08T03:54:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076596.html"},"modified":"2025-01-08T11:54:51","modified_gmt":"2025-01-08T03:54:51","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e4%b8%80%e4%b8%aa%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076596.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u4e00\u4e2a\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181106\/9b553853-3235-4202-9bac-490c8c1c28e9.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u4e00\u4e2a\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>Python\u5220\u9664\u4e00\u4e2a\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528del\u3001clear()\u3001pop()\u3001remove()\u3001\u5207\u7247\u8d4b\u7a7a\u5217\u8868\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u4e4b\u4e00\u662f\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u6765\u5220\u9664\u6574\u4e2a\u6570\u7ec4\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u4f7f\u7528\u793a\u4f8b\u3002<\/p>\n<\/p>\n<h2><strong>\u6b63\u6587<\/strong><\/h2>\n<p><h2>\u4e00\u3001\u4f7f\u7528del\u5173\u952e\u5b57<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u5220\u9664\u6574\u4e2a\u6570\u7ec4\uff0c\u6216\u8005\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>\u5220\u9664\u6574\u4e2a\u6570\u7ec4<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">arr = [1, 2, 3, 4, 5]<\/p>\n<p>del arr<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u7ec4<code>arr<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u5220\u9664\u4e86\u6574\u4e2a\u6570\u7ec4\u3002\u5220\u9664\u540e\uff0c\u5982\u679c\u4f60\u5c1d\u8bd5\u8bbf\u95ee<code>arr<\/code>\uff0c\u4f1a\u629b\u51fa\u4e00\u4e2a<code>NameError<\/code>\uff0c\u56e0\u4e3a\u8be5\u53d8\u91cf\u5df2\u7ecf\u4e0d\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><h3>\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">arr = [1, 2, 3, 4, 5]<\/p>\n<p>del arr[2]<\/p>\n<p>print(arr)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5220\u9664\u4e86\u6570\u7ec4\u4e2d\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20\uff08\u5373\u503c\u4e3a3\u7684\u5143\u7d20\uff09\uff0c\u7ed3\u679c\u6570\u7ec4\u53d8\u6210\u4e86<code>[1, 2, 4, 5]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528clear()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4e0d\u60f3\u5220\u9664\u6574\u4e2a\u6570\u7ec4\u5bf9\u8c61\uff0c\u4f46\u5e0c\u671b\u6e05\u7a7a\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>clear()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4f1a\u4fdd\u7559\u6570\u7ec4\u5bf9\u8c61\uff0c\u4f46\u5c06\u5176\u5185\u5bb9\u6e05\u7a7a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = [1, 2, 3, 4, 5]<\/p>\n<p>arr.clear()<\/p>\n<p>print(arr)  # \u8f93\u51fa: []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>clear()<\/code>\u65b9\u6cd5\u5c06\u6570\u7ec4<code>arr<\/code>\u4e2d\u7684\u6240\u6709\u5143\u7d20\u6e05\u7a7a\uff0c\u7ed3\u679c\u6570\u7ec4\u53d8\u6210\u4e86\u7a7a\u6570\u7ec4<code>[]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528pop()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>pop()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u7684\u503c\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u8981\u5220\u9664\u7684\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\u7d22\u5f15\uff0c\u9ed8\u8ba4\u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u5220\u9664\u5e76\u8fd4\u56de\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">arr = [1, 2, 3, 4, 5]<\/p>\n<p>removed_element = arr.pop(2)<\/p>\n<p>print(arr)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p>print(removed_element)  # \u8f93\u51fa: 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5220\u9664\u4e86\u6570\u7ec4\u4e2d\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20\uff08\u5373\u503c\u4e3a3\u7684\u5143\u7d20\uff09\uff0c\u5e76\u5c06\u5176\u503c\u8d4b\u7ed9\u53d8\u91cf<code>removed_element<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5220\u9664\u5e76\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u5143\u7d20<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">arr = [1, 2, 3, 4, 5]<\/p>\n<p>removed_element = arr.pop()<\/p>\n<p>print(arr)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p>print(removed_element)  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5220\u9664\u4e86\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff08\u5373\u503c\u4e3a5\u7684\u5143\u7d20\uff09\uff0c\u5e76\u5c06\u5176\u503c\u8d4b\u7ed9\u53d8\u91cf<code>removed_element<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528remove()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p><code>remove()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u6570\u7ec4\u4e2d\u6307\u5b9a\u503c\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u3002\u5982\u679c\u6570\u7ec4\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u503c\uff0c<code>remove()<\/code>\u65b9\u6cd5\u53ea\u4f1a\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = [1, 2, 3, 4, 5]<\/p>\n<p>arr.remove(3)<\/p>\n<p>print(arr)  # \u8f93\u51fa: [1, 2, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5220\u9664\u4e86\u6570\u7ec4\u4e2d\u503c\u4e3a3\u7684\u5143\u7d20\uff0c\u7ed3\u679c\u6570\u7ec4\u53d8\u6210\u4e86<code>[1, 2, 4, 5]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u5207\u7247\u8d4b\u7a7a\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u8bed\u6cd5\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u66ff\u6362\u4e3a\u7a7a\u5217\u8868\uff0c\u8fd9\u6837\u53ef\u4ee5\u6e05\u7a7a\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = [1, 2, 3, 4, 5]<\/p>\n<p>arr[:] = []<\/p>\n<p>print(arr)  # \u8f93\u51fa: []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u8bed\u6cd5\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u66ff\u6362\u4e3a\u7a7a\u5217\u8868\uff0c\u7ed3\u679c\u6570\u7ec4\u53d8\u6210\u4e86\u7a7a\u6570\u7ec4<code>[]<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528numpy\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528<code>numpy<\/code>\u5e93\u6765\u5904\u7406\u6570\u7ec4\u3002<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u5220\u9664\u6570\u7ec4\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u5220\u9664\u6574\u4e2a\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u5220\u9664<code>numpy<\/code>\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<p>del arr<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>numpy.delete()<\/code>\u51fd\u6570\u5220\u9664<code>numpy<\/code>\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<p>arr = np.delete(arr, 2)<\/p>\n<p>print(arr)  # \u8f93\u51fa: [1 2 4 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5220\u9664\u4e86<code>numpy<\/code>\u6570\u7ec4\u4e2d\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20\uff0c\u7ed3\u679c\u6570\u7ec4\u53d8\u6210\u4e86<code>[1, 2, 4, 5]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5220\u9664\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>numpy.delete()<\/code>\u51fd\u6570\u5220\u9664\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>arr = np.delete(arr, 1, axis=0)<\/p>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5220\u9664\u4e86\u591a\u7ef4\u6570\u7ec4\u4e2d\u7d22\u5f15\u4e3a1\u7684\u884c\uff0c\u7ed3\u679c\u6570\u7ec4\u53d8\u6210\u4e86\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1 2 3]<\/p>\n<p> [7 8 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5220\u9664\u6570\u7ec4\u6216\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u5305\u62ec\u4f7f\u7528<code>del<\/code>\u5173\u952e\u5b57\u3001<code>clear()<\/code>\u65b9\u6cd5\u3001<code>pop()<\/code>\u65b9\u6cd5\u3001<code>remove()<\/code>\u65b9\u6cd5\u3001\u5207\u7247\u8d4b\u7a7a\u5217\u8868\u7b49\u3002\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u63d0\u4f9b\u7684\u76f8\u5173\u51fd\u6570\u6765\u5220\u9664<code>numpy<\/code>\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6570\u7ec4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e2aPython\u547d\u4ee4\u6765\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>remove()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u6570\u7ec4\uff08\u6216\u5217\u8868\uff09\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u8be5\u65b9\u6cd5\u4f1a\u627e\u5230\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u5e76\u5c06\u5176\u5220\u9664\u3002\u5982\u679c\u4f60\u60f3\u8981\u6839\u636e\u7d22\u5f15\u5220\u9664\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u6216<code>pop()<\/code>\u65b9\u6cd5\uff0c\u540e\u8005\u4e0d\u4ec5\u4f1a\u5220\u9664\u5143\u7d20\uff0c\u8fd8\u4f1a\u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>2. \u5982\u679c\u6211\u60f3\u8981\u6e05\u7a7a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5e94\u8be5\u4f7f\u7528\u54ea\u4e2a\u65b9\u6cd5\uff1f<\/strong><br \/>\u8981\u6e05\u7a7a\u4e00\u4e2a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>clear()<\/code>\u65b9\u6cd5\u3002\u8fd9\u5c06\u4ece\u6570\u7ec4\u4e2d\u5220\u9664\u6240\u6709\u5143\u7d20\uff0c\u4f7f\u5176\u53d8\u4e3a\u7a7a\u6570\u7ec4\u3002\u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5c06\u6570\u7ec4\u91cd\u65b0\u8d4b\u503c\u4e3a\u7a7a\u5217\u8868\uff08\u4f8b\u5982<code>array = []<\/code>\uff09\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u6548\u679c\u3002<\/p>\n<p><strong>3. \u5728Python\u4e2d\uff0c\u5982\u4f55\u5220\u9664\u4e00\u4e2a\u5b8c\u6574\u7684\u6570\u7ec4\uff1f<\/strong><br \/>\u8981\u5220\u9664\u4e00\u4e2a\u5b8c\u6574\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u3002\u901a\u8fc7<code>del array_name<\/code>\uff0c\u4f60\u53ef\u4ee5\u5b8c\u5168\u79fb\u9664\u8be5\u6570\u7ec4\u53ca\u5176\u5f15\u7528\uff0c\u4ece\u5185\u5b58\u4e2d\u91ca\u653e\u76f8\u5173\u8d44\u6e90\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u65e6\u5220\u9664\uff0c\u65e0\u6cd5\u518d\u8bbf\u95ee\u8be5\u6570\u7ec4\uff0c\u6240\u4ee5\u8981\u786e\u4fdd\u5728\u5220\u9664\u4e4b\u524d\u5df2\u7ecf\u4e0d\u518d\u9700\u8981\u5b83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5220\u9664\u4e00\u4e2a\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528del\u3001clear()\u3001pop()\u3001remove()\u3001\u5207\u7247\u8d4b\u7a7a\u5217 [&hellip;]","protected":false},"author":3,"featured_media":1076611,"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\/1076596"}],"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=1076596"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076596\/revisions"}],"predecessor-version":[{"id":1076614,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076596\/revisions\/1076614"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076611"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}