{"id":3416,"date":"2025-06-03T15:35:15","date_gmt":"2025-06-04T07:52:14","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3416"},"modified":"2025-06-04T15:35:15","modified_gmt":"2025-06-04T07:52:14","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84del%e5%87%bd%e6%95%b8-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84del%e5%87%bd%e6%95%b8-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 del() \u51fd\u6578\uff1a\u6700\u4f73\u5be6\u8e10\u8207\u61c9\u7528\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, del(), Python \u6559\u5b78, del() \u51fd\u6578\u4f7f\u7528\"><\/p>\n<p>Python \u7684 `del()` \u51fd\u6578\u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u4f60\u522a\u9664\u7269\u4ef6\u3001\u5217\u8868\u3001\u5b57\u5178\u6216\u5176\u4ed6\u8cc7\u6599\u7d50\u69cb\u4e2d\u7684\u5143\u7d20\u3002\u900f\u904e\u6709\u6548\u7684\u8cc7\u6e90\u7ba1\u7406\uff0c`del()` \u51fd\u6578\u80fd\u8b93\u4f60\u4fdd\u6301\u8cc7\u6599\u7d50\u69cb\u7684\u6574\u6f54\u3002\u672c\u6587\u5c07\u6df1\u5165\u4ecb\u7d39 `del()` \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\u3001\u512a\u7f3a\u9ede\uff0c\u4ee5\u53ca\u4e00\u4e9b\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u4e26\u63d0\u4f9b\u932f\u8aa4\u6392\u9664\u548c\u5ef6\u4f38\u61c9\u7528\u7684\u5efa\u8b70\uff0c\u8b93\u4f60\u80fd\u5920\u5728 2025 \u5e74\u7684 Python \u7de8\u7a0b\u4e2d\u5145\u5206\u5229\u7528\u9019\u500b\u51fd\u6578\u3002<\/p>\n<h2>del() \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>`del()` \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u522a\u9664\u7269\u4ef6\u3001\u5217\u8868\u3001\u5b57\u5178\u6216\u5176\u4ed6\u8cc7\u6599\u7d50\u69cb\u4e2d\u7684\u5143\u7d20\u3002\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e00\u500b\u53c3\u6578\uff0c\u4e5f\u53ef\u4ee5\u63a5\u53d7\u591a\u500b\u53c3\u6578\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u7528\u6cd5\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u522a\u9664\u7269\u4ef6<br \/>\ndel my_object<\/p>\n<p># \u522a\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20<br \/>\nmy_list = [1, 2, 3, 4]\ndel my_list[2]  # \u522a\u9664\u7d22\u5f15\u70ba 2 \u7684\u5143\u7d20\uff0c\u7d50\u679c\u70ba [1, 2, 4]\n<p># \u522a\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20<br \/>\nmy_dict = {&#8216;a&#8217;: 1, &#8216;b&#8217;: 2, &#8216;c&#8217;: 3}<br \/>\ndel my_dict[&#8216;b&#8217;]  # \u522a\u9664\u9375 &#8216;b&#8217;\uff0c\u7d50\u679c\u70ba {&#8216;a&#8217;: 1, &#8216;c&#8217;: 3}<br \/>\n&#8220;`<\/p>\n<h2>del() \u51fd\u6578\u7684\u512a\u9ede<\/h2>\n<p>`del()` \u51fd\u6578\u7684\u4e3b\u8981\u512a\u9ede\u5728\u65bc\u5b83\u80fd\u6709\u6548\u5730\u7ba1\u7406\u548c\u6e05\u7406\u8cc7\u6599\u3002\u4f7f\u7528 `del()` \u53ef\u4ee5\u91cb\u653e\u4e0d\u518d\u9700\u8981\u7684\u8a18\u61b6\u9ad4\u7a7a\u9593\uff0c\u5c24\u5176\u662f\u5728\u8655\u7406\u5927\u91cf\u8cc7\u6599\u6642\uff0c\u9019\u80fd\u63d0\u9ad8\u7a0b\u5f0f\u7684\u57f7\u884c\u6548\u7387\u3002<\/p>\n<h2>del() \u51fd\u6578\u7684\u7f3a\u9ede<\/h2>\n<p>\u96d6\u7136 `del()` \u51fd\u6578\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5b83\u4e5f\u6709\u4e00\u4e9b\u6f5b\u5728\u7684\u7f3a\u9ede\u3002\u6700\u4e3b\u8981\u7684\u554f\u984c\u662f\u8cc7\u6599\u7d50\u69cb\u7684\u4e00\u81f4\u6027\u3002\u5728\u522a\u9664\u67d0\u500b\u5143\u7d20\u5f8c\uff0c\u8a72\u5143\u7d20\u4e0d\u518d\u5b58\u5728\u65bc\u8cc7\u6599\u7d50\u69cb\u4e2d\uff0c\u9019\u53ef\u80fd\u6703\u5c0e\u81f4\u7a0b\u5f0f\u5728\u5f8c\u7e8c\u64cd\u4f5c\u6642\u51fa\u73fe\u932f\u8aa4\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528 `del()` \u51fd\u6578\u6642\uff0c\u52d9\u5fc5\u5c0f\u5fc3\uff0c\u78ba\u4fdd\u4e0d\u6703\u522a\u9664\u5fc5\u8981\u7684\u8cc7\u6599\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `del()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **KeyError**\uff1a\u7576\u4f60\u5617\u8a66\u522a\u9664\u4e00\u500b\u4e0d\u5b58\u5728\u65bc\u5b57\u5178\u4e2d\u7684\u9375\u6642\uff0c\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u89e3\u6c7a\u65b9\u6cd5\u662f\u5148\u78ba\u8a8d\u8a72\u9375\u662f\u5426\u5b58\u5728\u3002<\/p>\n<p>  &#8220;`python<br \/>\n  if &#8216;b&#8217; in my_dict:<br \/>\n      del my_dict[&#8216;b&#8217;]\n  &#8220;`<\/p>\n<p>&#8211; **IndexError**\uff1a\u7576\u4f60\u5617\u8a66\u522a\u9664\u4e00\u500b\u5217\u8868\u4e2d\u4e0d\u5b58\u5728\u7684\u7d22\u5f15\u6642\uff0c\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u5efa\u8b70\u5728\u522a\u9664\u4e4b\u524d\u6aa2\u67e5\u5217\u8868\u7684\u9577\u5ea6\u3002<\/p>\n<p>  &#8220;`python<br \/>\n  if len(my_list) > 2:<br \/>\n      del my_list[2]\n  &#8220;`<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`del()` \u51fd\u6578\u4e0d\u50c5\u53ef\u4ee5\u7528\u4f86\u522a\u9664\u5143\u7d20\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u66f4\u8907\u96dc\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5982\u4e8c\u7dad\u5217\u8868\u6216\u5d4c\u5957\u5b57\u5178\u3002\u8209\u4f8b\u4f86\u8aaa\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u522a\u9664\u4e8c\u7dad\u5217\u8868\u4e2d\u7684\u5143\u7d20<br \/>\nmatrix = [[1, 2, 3], [4, 5, 6]]\ndel matrix[0][1]  # \u522a\u9664\u7b2c\u4e00\u884c\u7684\u7b2c\u4e8c\u500b\u5143\u7d20\uff0c\u7d50\u679c\u70ba [[1, 3], [4, 5, 6]]\n&#8220;`<\/p>\n<p>\u6b32\u4e86\u89e3\u66f4\u591a Python \u6559\u5b78\uff0c\u53ef\u4ee5\u53c3\u8003 [vocus.cc \u7684 Python \u7cfb\u5217\u6587\u7ae0](https:\/\/vocus.cc) \u6216 [miner.tw \u7684 Python \u8cc7\u6e90](https:\/\/miner.tw)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u4f7f\u7528 `del()` \u51fd\u6578\u522a\u9664\u7269\u4ef6\u5f8c\uff0c\u5982\u4f55\u78ba\u8a8d\u8a72\u7269\u4ef6\u5df2\u7d93\u88ab\u522a\u9664\uff1f**<br \/>\nA1: \u4f60\u53ef\u4ee5\u4f7f\u7528 `try&#8230;except` \u8a9e\u53e5\u4f86\u6355\u6349\u5c0d\u5df2\u522a\u9664\u7269\u4ef6\u7684\u5f15\u7528\uff0c\u5f9e\u800c\u78ba\u8a8d\u7269\u4ef6\u5df2\u7d93\u88ab\u6210\u529f\u522a\u9664\u3002<\/p>\n<p>**Q2: \u662f\u5426\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u522a\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1f**<br \/>\nA2: \u9664\u4e86 `del()` \u51fd\u6578\uff0c\u4f60\u9084\u53ef\u4ee5\u4f7f\u7528 `remove()` \u65b9\u6cd5\u6216 `pop()` \u65b9\u6cd5\u4f86\u522a\u9664\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002`remove()` \u6703\u6839\u64da\u503c\u522a\u9664\uff0c\u800c `pop()` \u5247\u6839\u64da\u7d22\u5f15\u522a\u9664\u3002<\/p>\n<p>**Q3: \u5728\u522a\u9664\u5b57\u5178\u5143\u7d20\u6642\uff0c\u80fd\u5426\u4f7f\u7528 `del()` \u51fd\u6578\u522a\u9664\u591a\u500b\u9375\uff1f**<br \/>\nA3: `del()` \u51fd\u6578\u672c\u8eab\u4e0d\u652f\u63f4\u522a\u9664\u591a\u500b\u9375\uff0c\u4f46\u4f60\u53ef\u4ee5\u4f7f\u7528\u8ff4\u5708\u4f86\u904d\u6b77\u9700\u8981\u522a\u9664\u7684\u9375\uff0c\u9010\u4e00\u522a\u9664\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>del()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u4f86\u522a\u9664\u7269\u4ef6\u7684\u5f15\u7528\uff0c\u4ee5\u53ca\u522a\u9664\u8b8a\u6578\uff0c\u8b93\u4f60\u7684\u7a0b\u5f0f\u66f4\u52a0\u9748\u6d3b\u3002\u672c\u6587\u5c07\u4ecb\u7d39del()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-T6","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3416"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3416\/revisions"}],"predecessor-version":[{"id":3417,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3416\/revisions\/3417"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}