{"id":2733,"date":"2025-05-31T17:43:19","date_gmt":"2025-06-04T09:47:19","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2733"},"modified":"2025-06-04T17:43:19","modified_gmt":"2025-06-04T09:47:19","slug":"%e4%bd%bf%e7%94%a8python%e7%9a%84enumerate%e6%96%b9%e6%b3%95%e9%81%8d%e6%ad%b7%e5%ba%8f%e5%88%97%e4%b8%ad%e7%9a%84%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%bd%bf%e7%94%a8python%e7%9a%84enumerate%e6%96%b9%e6%b3%95%e9%81%8d%e6%ad%b7%e5%ba%8f%e5%88%97%e4%b8%ad%e7%9a%84%e5%85%83%e7%b4%a0\/","title":{"rendered":"\u5168\u9762\u89e3\u6790 Python \u7684 enumerate() \u65b9\u6cd5\uff1a\u9ad8\u6548\u904d\u6b77\u5e8f\u5217\u7684\u6700\u4f73\u5be6\u8e10\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, enumerate(), Python \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h1>\u5168\u9762\u89e3\u6790 Python \u7684 enumerate() \u65b9\u6cd5\uff1a\u9ad8\u6548\u904d\u6b77\u5e8f\u5217\u7684\u6700\u4f73\u5be6\u8e10\uff082025 \u6700\u65b0\u7248\uff09<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`enumerate()` \u65b9\u6cd5\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u80fd\u8b93\u6211\u5011\u5728\u904d\u6b77\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7d44\u3001\u5b57\u4e32\u53ca\u5b57\u5178\uff09\u6642\uff0c\u540c\u6642\u7372\u53d6\u5143\u7d20\u7684\u7d22\u5f15\u503c\u3002\u9019\u4e0d\u50c5\u63d0\u5347\u4e86\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\uff0c\u9084\u80fd\u8b93\u6211\u5011\u66f4\u6709\u6548\u7387\u5730\u8655\u7406\u6578\u64da\u3002<\/p>\n<p><!--more--><\/p>\n<p>### enumerate() \u65b9\u6cd5\u7684\u8a9e\u6cd5<\/p>\n<p>`enumerate()` \u65b9\u6cd5\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nenumerate(sequence, start=0)<br \/>\n&#8220;`<\/p>\n<p>&#8211; **sequence**: \u53ef\u4ee5\u662f\u4e00\u500b list\u3001tuple\u3001string \u6216 dictionary\u3002<br \/>\n&#8211; **start**: \u6307\u5b9a\u7d22\u5f15\u503c\u7684\u8d77\u59cb\u503c\uff0c\u9810\u8a2d\u70ba 0\u3002<\/p>\n<p>### \u4f7f\u7528\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 `enumerate()` \u65b9\u6cd5\u904d\u6b77\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfruits = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;]\nfor index, fruit in enumerate(fruits):<br \/>\n    print(&#8220;index: {}, fruit: {}&#8221;.format(index, fruit))<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`plaintext<br \/>\nindex: 0, fruit: apple<br \/>\nindex: 1, fruit: banana<br \/>\nindex: 2, fruit: cherry<br \/>\n&#8220;`<\/p>\n<p>### \u5be6\u4f5c\u89e3\u91cb<\/p>\n<p>\u5728\u4e0a\u8ff0\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u6c34\u679c\u5217\u8868 `fruits`\uff0c\u7136\u5f8c\u4f7f\u7528 `enumerate()` \u65b9\u6cd5\u904d\u6b77\u8a72\u5217\u8868\u3002\u6bcf\u6b21\u8fed\u4ee3\u6642\uff0c`index` \u8b8a\u6578\u6703\u63a5\u6536\u7576\u524d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u800c `fruit` \u8b8a\u6578\u5247\u63a5\u6536\u7576\u524d\u5143\u7d20\u7684\u503c\u3002\u9019\u6a23\u7684\u65b9\u5f0f\u4f7f\u5f97\u6211\u5011\u80fd\u5920\u7c21\u55ae\u800c\u6e05\u6670\u5730\u7372\u5f97\u6bcf\u500b\u5143\u7d20\u53ca\u5176\u4f4d\u7f6e\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u7576\u4f7f\u7528 `enumerate()` \u65b9\u6cd5\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<p>1. **TypeError**: \u5982\u679c\u50b3\u905e\u7684\u53c3\u6578\u4e0d\u662f\u53ef\u8fed\u4ee3\u5c0d\u8c61\uff0c\u5247\u6703\u5f15\u767c\u932f\u8aa4\u3002\u8acb\u78ba\u4fdd\u50b3\u5165\u7684\u53c3\u6578\u662f list\u3001tuple\u3001string \u6216 dictionary\u3002<br \/>\n2. **IndexError**: \u5982\u679c\u5728\u904d\u6b77\u904e\u7a0b\u4e2d\u5617\u8a66\u8a2a\u554f\u7d22\u5f15\u8d85\u51fa\u7bc4\u570d\u7684\u5143\u7d20\uff0c\u5247\u6703\u5f15\u767c\u932f\u8aa4\u3002\u78ba\u4fdd\u5728\u8fed\u4ee3\u6642\u4e0d\u6703\u8d85\u904e\u5e8f\u5217\u7684\u9577\u5ea6\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`enumerate()` \u65b9\u6cd5\u5728\u8655\u7406\u6578\u64da\u6642\u7279\u5225\u6709\u7528\uff0c\u4f8b\u5982\u5728\u9700\u8981\u7d22\u5f15\u7684\u60c5\u6cc1\u4e0b\uff0c\u5982\u5728\u6578\u64da\u5206\u6790\u6216\u6a5f\u5668\u5b78\u7fd2\u4e2d\u3002\u9019\u88e1\u662f\u4e00\u500b\u9032\u968e\u7bc4\u4f8b\uff0c\u4f7f\u7528 `enumerate()` \u65b9\u6cd5\u4f86\u5efa\u7acb\u4e00\u500b\u5b57\u5178\uff0c\u5c07\u6c34\u679c\u53ca\u5176\u7d22\u5f15\u503c\u5b58\u5132\u8d77\u4f86\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfruit_dict = {index: fruit for index, fruit in enumerate(fruits)}<br \/>\nprint(fruit_dict)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u53ef\u4ee5\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`plaintext<br \/>\n{0: &#8216;apple&#8217;, 1: &#8216;banana&#8217;, 2: &#8216;cherry&#8217;}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u7a2e\u65b9\u6cd5\u80fd\u5feb\u901f\u69cb\u5efa\u4e00\u500b\u7d22\u5f15\u5b57\u5178\uff0c\u5c0d\u65bc\u5f8c\u7e8c\u7684\u6578\u64da\u8655\u7406\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u9032\u968e\u7528\u6cd5\uff0c\u8acb\u53c3\u8003\u6211\u5011\u7684[Python \u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc), \u9019\u88e1\u6709\u66f4\u591a\u6280\u5de7\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: enumerate() \u65b9\u6cd5\u7684 start \u53c3\u6578\u53ef\u4ee5\u8a2d\u7f6e\u70ba\u8ca0\u6578\u55ce\uff1f**<br \/>\nA1: \u662f\u7684\uff0c`start` \u53c3\u6578\u53ef\u4ee5\u8a2d\u7f6e\u70ba\u8ca0\u6578\uff0c\u9019\u6a23\u7d22\u5f15\u5c07\u5f9e\u8ca0\u503c\u958b\u59cb\u8a08\u7b97\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5728\u5b57\u5178\u4e2d\u4f7f\u7528 enumerate()?**<br \/>\nA2: \u96d6\u7136 `enumerate()` \u4e3b\u8981\u7528\u65bc\u5217\u8868\u7b49\u5e8f\u5217\uff0c\u4f46\u53ef\u4ee5\u5c07\u5b57\u5178\u7684\u9375\u6216\u503c\u8f49\u63db\u70ba\u5217\u8868\u5f8c\u4f7f\u7528\uff0c\u6216\u8005\u76f4\u63a5\u8fed\u4ee3\u5b57\u5178\u7684\u9805\u76ee\u3002<\/p>\n<p>**Q3: \u4f7f\u7528 enumerate() \u65b9\u6cd5\u6703\u5f71\u97ff\u539f\u59cb\u5e8f\u5217\u55ce\uff1f**<br \/>\nA3: \u4e0d\u6703\uff0c`enumerate()` \u50c5\u5728\u904d\u6b77\u5e8f\u5217\u6642\u63d0\u4f9b\u7d22\u5f15\uff0c\u4e26\u4e0d\u6703\u4fee\u6539\u539f\u59cb\u5e8f\u5217\u7684\u5167\u5bb9\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Python\u7684enumerate()\u65b9\u6cd5\u4f86\u904d\u6b77\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39enumerate()\u65b9\u6cd5\u7684\u4f7f\u7528\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-2733","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-I5","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2733","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=2733"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2733\/revisions"}],"predecessor-version":[{"id":2734,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2733\/revisions\/2734"}],"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=2733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}