{"id":2672,"date":"2025-06-02T17:34:06","date_gmt":"2025-06-04T09:56:06","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2672"},"modified":"2025-06-04T17:34:06","modified_gmt":"2025-06-04T09:56:06","slug":"%e4%bd%bf%e7%94%a8enumerate%e5%87%bd%e6%95%b8%e5%9c%a8python%e4%b8%b2%e5%88%97%e4%b8%ad%e5%8f%96%e5%be%97%e5%85%83%e7%b4%a0%e7%b4%a2%e5%bc%95%e5%92%8c%e5%80%bc","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8enumerate%e5%87%bd%e6%95%b8%e5%9c%a8python%e4%b8%b2%e5%88%97%e4%b8%ad%e5%8f%96%e5%be%97%e5%85%83%e7%b4%a0%e7%b4%a2%e5%bc%95%e5%92%8c%e5%80%bc\/","title":{"rendered":"2025 \u6700\u65b0\u7248\uff1a\u4f7f\u7528 `enumerate()` \u51fd\u6578\u5728 Python \u4e32\u5217\u4e2d\u8f15\u9b06\u53d6\u5f97\u5143\u7d20\u7d22\u5f15\u548c\u503c"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, \u4e32\u5217, List, enumerate, \u51fd\u6578, \u8ff4\u5708, \u7d22\u5f15, \u503c\"><\/p>\n<h1>2025 \u6700\u65b0\u7248\uff1a\u5229\u7528 Python \u4e32\u5217(List)\u4f7f\u7528 `enumerate()` \u51fd\u6578\u5728\u8ff4\u5708\u4e2d\u540c\u6642\u53d6\u5f97\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c<\/h1>\n<p>Python \u4e32\u5217(List)\u662f\u4e00\u7a2e\u5e38\u7528\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5b83\u53ef\u4ee5\u5132\u5b58\u591a\u500b\u5143\u7d20\u3002\u800c `enumerate()` \u51fd\u6578\u5247\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5728\u8ff4\u5708\u4e2d\u540c\u6642\u53d6\u5f97\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\uff0c\u8b93\u6211\u5011\u7684\u7a0b\u5f0f\u78bc\u66f4\u52a0\u7c21\u6f54\u4e14\u6613\u65bc\u7406\u89e3\u3002<\/p>\n<p>## `enumerate()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/p>\n<p>`enumerate()` \u51fd\u6578\u53ef\u4ee5\u5c07\u4e00\u500b\u53ef\u8fed\u4ee3\u7684\u7269\u4ef6\uff08\u5982\u4e32\u5217\uff09\u8f49\u63db\u70ba\u7d22\u5f15\u5e8f\u5217\uff0c\u540c\u6642\u5217\u51fa\u6578\u503c\u548c\u5c0d\u61c9\u7684\u7d22\u5f15\u503c\uff0c\u901a\u5e38\u61c9\u7528\u65bc `for` \u8ff4\u5708\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u4e32\u5217<br \/>\nlist1 = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;]\n<p># \u4f7f\u7528 enumerate() \u51fd\u6578<br \/>\nfor index, value in enumerate(list1):<br \/>\n    print(index, value)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`<br \/>\n0 apple<br \/>\n1 banana<br \/>\n2 cherry<br \/>\n&#8220;`<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c`enumerate()` \u51fd\u6578\u5c07\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u8f49\u63db\u70ba\u7d22\u5f15\u5e8f\u5217\uff0c\u540c\u6642\u986f\u793a\u6578\u503c\u548c\u5c0d\u61c9\u7684\u7d22\u5f15\u503c\uff0c\u9019\u6a23\u6211\u5011\u5c31\u80fd\u5728\u8ff4\u5708\u4e2d\u540c\u6642\u53d6\u5f97\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\u3002<\/p>\n<p>## \u6307\u5b9a\u7d22\u5f15\u7684\u8d77\u59cb\u503c<\/p>\n<p>`enumerate()` \u51fd\u6578\u4e5f\u53ef\u4ee5\u6307\u5b9a\u7d22\u5f15\u7684\u8d77\u59cb\u503c\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u4e32\u5217<br \/>\nlist1 = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;]\n<p># \u4f7f\u7528 enumerate() \u51fd\u6578\uff0c\u6307\u5b9a\u7d22\u5f15\u7684\u8d77\u59cb\u503c\u70ba 10<br \/>\nfor index, value in enumerate(list1, 10):<br \/>\n    print(index, value)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`<br \/>\n10 apple<br \/>\n11 banana<br \/>\n12 cherry<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\uff0c\u6211\u5011\u5c31\u80fd\u9748\u6d3b\u5730\u6307\u5b9a\u7d22\u5f15\u7684\u8d77\u59cb\u503c\uff0c\u8b93\u8ff4\u5708\u4e2d\u7684\u7d22\u5f15\u66f4\u5177\u610f\u7fa9\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `enumerate()` \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **TypeError**\uff1a\u5982\u679c\u50b3\u905e\u7d66 `enumerate()` \u7684\u5c0d\u8c61\u4e0d\u662f\u53ef\u8fed\u4ee3\u7684\uff0c\u5c07\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u78ba\u4fdd\u50b3\u905e\u7684\u662f\u4e32\u5217\u3001\u5143\u7d44\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5c0d\u8c61\u3002<\/p>\n<p>2. **IndexError**\uff1a\u5728\u8a2a\u554f\u5217\u8868\u5143\u7d20\u6642\uff0c\u5982\u679c\u7d22\u5f15\u8d85\u51fa\u7bc4\u570d\uff0c\u5c07\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u78ba\u8a8d\u7d22\u5f15\u4e0d\u8d85\u904e\u5217\u8868\u7684\u9577\u5ea6\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`enumerate()` \u51fd\u6578\u4e0d\u50c5\u9069\u7528\u65bc\u4e32\u5217\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u5176\u4ed6\u53ef\u8fed\u4ee3\u5c0d\u8c61\uff0c\u5982\u5143\u7d44\u548c\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528\u5b57\u5178\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u5b57\u5178<br \/>\ndict1 = {&#8220;name&#8221;: &#8220;Alice&#8221;, &#8220;age&#8221;: 30, &#8220;city&#8221;: &#8220;New York&#8221;}<\/p>\n<p># \u4f7f\u7528 enumerate() \u51fd\u6578\u8207\u5b57\u5178\u7684\u9805\u76ee<br \/>\nfor index, (key, value) in enumerate(dict1.items()):<br \/>\n    print(index, key, value)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`<br \/>\n0 name Alice<br \/>\n1 age 30<br \/>\n2 city New York<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 `enumerate()` \u4f86\u7372\u53d6\u5b57\u5178\u9805\u76ee\u7684\u7d22\u5f15\u3001\u9375\u548c\u503c\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u8655\u7406\u66f4\u52a0\u9748\u6d3b\u3002<\/p>\n<p>## \u7e3d\u7d50<\/p>\n<p>`enumerate()` \u51fd\u6578\u662f Python \u4e2d\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5c07\u53ef\u8fed\u4ee3\u7684\u7269\u4ef6\uff08\u5982\u4e32\u5217\uff09\u8f49\u63db\u70ba\u7d22\u5f15\u5e8f\u5217\uff0c\u8b93\u6211\u5011\u5728\u8ff4\u5708\u4e2d\u8f15\u9b06\u540c\u6642\u53d6\u5f97\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\u3002\u900f\u904e\u6307\u5b9a\u7d22\u5f15\u7684\u8d77\u59cb\u503c\uff0c\u6211\u5011\u80fd\u5920\u66f4\u9748\u6d3b\u5730\u63a7\u5236\u7d22\u5f15\u7684\u986f\u793a\uff0c\u4f7f\u7a0b\u5f0f\u78bc\u66f4\u52a0\u6e05\u6670\u6613\u61c2\u3002<\/p>\n<p>\u6b32\u4e86\u89e3\u66f4\u591a Python \u51fd\u6578\u7684\u8a73\u7d30\u4f7f\u7528\u65b9\u6cd5\uff0c\u8acb\u53c3\u8003 [\u9019\u88e1](https:\/\/vocus.cc) \u7684\u76f8\u95dc\u6559\u5b78\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `enumerate()` \u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA1: `enumerate()` \u51fd\u6578\u8fd4\u56de\u7684\u662f\u4e00\u500b\u53ef\u8fed\u4ee3\u7684 enumerate \u5c0d\u8c61\uff0c\u5176\u4e2d\u6bcf\u4e00\u9805\u90fd\u662f\u4e00\u500b\u5143\u7d44\uff0c\u5305\u542b\u7d22\u5f15\u548c\u5143\u7d20\u3002<\/p>\n<p>**Q2: \u6211\u53ef\u4ee5\u5728 `enumerate()` \u4e2d\u4f7f\u7528\u5176\u4ed6\u53ef\u8fed\u4ee3\u7269\u4ef6\u55ce\uff1f**<br \/>\nA2: \u662f\u7684\uff0c`enumerate()` \u53ef\u4ee5\u7528\u65bc\u4efb\u4f55\u53ef\u8fed\u4ee3\u7684\u5c0d\u8c61\uff0c\u5305\u62ec\u5143\u7d44\u3001\u5b57\u5178\u548c\u96c6\u5408\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u5728 Python \u4e2d\u4f7f\u7528 `enumerate()` \u4f86\u8655\u7406\u5927\u578b\u6578\u64da\u96c6\uff1f**<br \/>\nA3: \u7576\u8655\u7406\u5927\u578b\u6578\u64da\u96c6\u6642\uff0c\u53ef\u4ee5\u7d50\u5408 `enumerate()` \u8207\u751f\u6210\u5668\uff0c\u6e1b\u5c11\u5167\u5b58\u4f7f\u7528\uff0c\u6216\u642d\u914d\u5176\u4ed6\u51fd\u6578\u5982 `filter()` \u9032\u884c\u689d\u4ef6\u7be9\u9078\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aPython\u4e32\u5217(List)\u4e2d\u7684enumerate()\u51fd\u6578\u53ef\u4ee5\u5728\u8ff4\u5708\u4e2d\u540c\u6642\u53d6\u5f97\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u7684\u8655\u7406\u8cc7\u6599\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-2672","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-H6","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2672","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=2672"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2672\/revisions"}],"predecessor-version":[{"id":13049,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2672\/revisions\/13049"}],"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=2672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}