{"id":3462,"date":"2025-06-03T15:39:11","date_gmt":"2025-06-04T07:40:11","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3462"},"modified":"2025-06-04T15:39:11","modified_gmt":"2025-06-04T07:40:11","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84index%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84index%e5%87%bd%e6%95%b8%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 index() \u51fd\u6578\uff1a\u7528\u6cd5\u3001\u7bc4\u4f8b\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, index(), Python \u6559\u5b78, \u5b57\u4e32\u64cd\u4f5c, Python \u5b57\u4e32\u641c\u5c0b\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 index() \u51fd\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`index()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u5b57\u4e32\u4e2d\u641c\u5c0b\u7279\u5b9a\u5b57\u5143\u6216\u5b50\u5b57\u4e32\u7684\u4f4d\u7f6e\u3002\u9019\u500b\u51fd\u6578\u6703\u56de\u50b3\u7b26\u5408\u689d\u4ef6\u7684\u7b2c\u4e00\u500b\u5b57\u5143\u7684\u7d22\u5f15\u503c\uff0c\u5982\u679c\u627e\u4e0d\u5230\uff0c\u5247\u6703\u5f15\u767c `ValueError`\u3002\u672c\u6587\u5c07\u4ecb\u7d39 `index()` \u51fd\u6578\u7684\u6700\u65b0\u8a9e\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\u3001\u5e38\u898b\u932f\u8aa4\u6392\u9664\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u5145\u5206\u5229\u7528\u9019\u500b\u51fd\u6578\u3002<\/p>\n<p>### `index()` \u51fd\u6578\u8a9e\u6cd5<\/p>\n<p>`index()` \u51fd\u6578\u7684\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nstr.index(sub[, start[, end]])<br \/>\n&#8220;`<\/p>\n<p>&#8211; **str**\uff1a\u8981\u641c\u5c0b\u7684\u5b57\u4e32\u3002<br \/>\n&#8211; **sub**\uff1a\u8981\u641c\u5c0b\u7684\u5b57\u5143\u6216\u5b50\u5b57\u4e32\u3002<br \/>\n&#8211; **start**\uff08\u53ef\u9078\uff09\uff1a\u958b\u59cb\u641c\u5c0b\u7684\u4f4d\u7f6e\u3002<br \/>\n&#8211; **end**\uff08\u53ef\u9078\uff09\uff1a\u7d50\u675f\u641c\u5c0b\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>### \u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>\u63a5\u4e0b\u4f86\uff0c\u8b93\u6211\u5011\u770b\u4e00\u500b\u5177\u9ad4\u7684\u7bc4\u4f8b\uff0c\u4f86\u66f4\u597d\u5730\u7406\u89e3 `index()` \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u5f0f\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntext = &#8220;Hello World&#8221;<\/p>\n<p># \u641c\u5c0b\u5b57\u5143 &#8220;o&#8221;<br \/>\nfirst_index = text.index(&#8220;o&#8221;)<br \/>\nprint(&#8220;\u7b2c\u4e00\u500b &#8216;o&#8217; \u7684\u7d22\u5f15\u503c:&#8221;, first_index)<\/p>\n<p># \u641c\u5c0b\u5b57\u5143 &#8220;o&#8221; \u5f9e\u7d22\u5f15\u503c 4 \u958b\u59cb<br \/>\nsecond_index = text.index(&#8220;o&#8221;, 4)<br \/>\nprint(&#8220;\u7b2c\u4e8c\u500b &#8216;o&#8217; \u7684\u7d22\u5f15\u503c:&#8221;, second_index)<\/p>\n<p># \u5617\u8a66\u5f9e\u7d22\u5f15\u503c 4 \u5230 8 \u4e4b\u9593\u641c\u5c0b\u5b57\u5143 &#8220;o&#8221;<br \/>\ntry:<br \/>\n    third_index = text.index(&#8220;o&#8221;, 4, 8)<br \/>\n    print(&#8220;\u7b2c\u4e09\u500b &#8216;o&#8217; \u7684\u7d22\u5f15\u503c:&#8221;, third_index)<br \/>\nexcept ValueError:<br \/>\n    print(&#8220;\u5728\u6307\u5b9a\u7bc4\u570d\u5167\u627e\u4e0d\u5230 &#8216;o&#8217;\u3002&#8221;)<br \/>\n&#8220;`<\/p>\n<p>#### \u57f7\u884c\u7d50\u679c<\/p>\n<p>&#8220;`<br \/>\n\u7b2c\u4e00\u500b &#8216;o&#8217; \u7684\u7d22\u5f15\u503c: 4<br \/>\n\u7b2c\u4e8c\u500b &#8216;o&#8217; \u7684\u7d22\u5f15\u503c: 7<br \/>\n\u5728\u6307\u5b9a\u7bc4\u570d\u5167\u627e\u4e0d\u5230 &#8216;o&#8217;\u3002<br \/>\n&#8220;`<\/p>\n<p>\u5f9e\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u770b\u5230 `index()` \u51fd\u6578\u662f\u5982\u4f55\u5728\u5b57\u4e32\u4e2d\u641c\u5c0b\u7279\u5b9a\u5b57\u5143\u7684\u3002\u6ce8\u610f\uff0c\u5728\u641c\u5c0b\u7bc4\u570d\u5167\u5982\u679c\u627e\u4e0d\u5230\u5b57\u5143\uff0c\u6703\u5f15\u767c `ValueError`\uff0c\u56e0\u6b64\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `try-except` \u4f86\u6355\u6349\u9019\u500b\u932f\u8aa4\u3002<\/p>\n<p>### `index()` \u51fd\u6578\u7684\u61c9\u7528<\/p>\n<p>`index()` \u51fd\u6578\u5728\u5b57\u4e32\u8655\u7406\u4e2d\u6709\u591a\u7a2e\u61c9\u7528\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u4f8b\u5b50\uff1a<\/p>\n<p>1. **\u641c\u5c0b\u7a7a\u683c\u7684\u4f4d\u7f6e**\uff1a\u53ef\u4ee5\u7528\u4f86\u627e\u5230\u5b57\u4e32\u4e2d\u7684\u7b2c\u4e00\u500b\u7a7a\u683c\u4f4d\u7f6e\uff0c\u4ee5\u4fbf\u5c0d\u5b57\u4e32\u9032\u884c\u5206\u5272\u3002<br \/>\n2. **\u622a\u53d6\u5b57\u4e32**\uff1a\u900f\u904e\u641c\u5c0b\u7279\u5b9a\u5b57\u5143\u7684\u4f4d\u7f6e\u4f86\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u67d0\u4e00\u90e8\u5206\u3002<br \/>\n3. **\u7d71\u8a08\u5b57\u5143\u51fa\u73fe\u6b21\u6578**\uff1a\u96d6\u7136 `index()` \u672c\u8eab\u4e0d\u63d0\u4f9b\u9019\u500b\u529f\u80fd\uff0c\u4f46\u6211\u5011\u53ef\u4ee5\u7d50\u5408\u5176\u4ed6\u529f\u80fd\u4f86\u8a08\u7b97\u7279\u5b9a\u5b57\u5143\u6216\u5b50\u5b57\u4e32\u7684\u51fa\u73fe\u6b21\u6578\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u662f\u4f7f\u7528 `index()` \u51fd\u6578\u53ca\u8ff4\u5708\u4f86\u8a08\u7b97\u7279\u5b9a\u5b57\u5143\u5728\u5b57\u4e32\u4e2d\u51fa\u73fe\u6b21\u6578\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntext = &#8220;Hello World&#8221;<br \/>\nchar_to_find = &#8220;o&#8221;<br \/>\ncount = 0<br \/>\nstart = 0<\/p>\n<p>while True:<br \/>\n    try:<br \/>\n        start = text.index(char_to_find, start)<br \/>\n        count += 1<br \/>\n        start += 1  # \u79fb\u52d5\u5230\u4e0b\u4e00\u500b\u4f4d\u7f6e<br \/>\n    except ValueError:<br \/>\n        break<\/p>\n<p>print(f&#8221;\u5b57\u5143 &#8216;{char_to_find}&#8217; \u5728\u5b57\u4e32\u4e2d\u51fa\u73fe\u7684\u6b21\u6578: {count}&#8221;)<br \/>\n&#8220;`<\/p>\n<p>### \u5e38\u898b\u932f\u8aa4\u8207\u6392\u9664<\/p>\n<p>&#8211; **ValueError**\uff1a\u7576\u641c\u5c0b\u7684\u5b50\u5b57\u4e32\u4e0d\u5728\u76ee\u6a19\u5b57\u4e32\u4e2d\u6642\uff0c`index()` \u6703\u5f15\u767c `ValueError`\u3002\u5728\u7de8\u5beb\u7a0b\u5f0f\u6642\uff0c\u61c9\u8003\u616e\u4f7f\u7528 `try-except` \u4f86\u8655\u7406\u9019\u500b\u932f\u8aa4\u3002<br \/>\n&#8211; **\u7d22\u5f15\u7bc4\u570d\u554f\u984c**\uff1a\u5982\u679c\u8a2d\u5b9a\u7684 `start` \u6216 `end` \u8d85\u51fa\u4e86\u5b57\u4e32\u7684\u9577\u5ea6\uff0c\u5c07\u6703\u5f15\u767c\u932f\u8aa4\uff0c\u61c9\u7576\u6aa2\u67e5\u9019\u4e9b\u53c3\u6578\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u82e5\u60f3\u8981\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7684\u5b57\u4e32\u8655\u7406\u6280\u5de7\uff0c\u5efa\u8b70\u53c3\u8003\u9019\u7bc7\u6587\u7ae0 [Python \u5b57\u4e32\u64cd\u4f5c\u5165\u9580](https:\/\/vocus.cc) \u4f86\u64f4\u5c55\u4f60\u7684\u77e5\u8b58\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `index()` \u51fd\u6578\u8207 `find()` \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: `index()` \u51fd\u6578\u5728\u627e\u4e0d\u5230\u5b50\u5b57\u4e32\u6642\u6703\u5f15\u767c `ValueError`\uff0c\u800c `find()` \u51fd\u6578\u5247\u6703\u56de\u50b3 -1\u3002\u9078\u64c7\u4f7f\u7528\u54ea\u4e00\u500b\u53d6\u6c7a\u65bc\u4f60\u7684\u9700\u6c42\u3002<\/p>\n<p>**Q2: \u6211\u53ef\u4ee5\u4f7f\u7528 `index()` \u51fd\u6578\u4f86\u641c\u5c0b\u591a\u500b\u5b57\u5143\u55ce\uff1f**<br \/>\nA2: `index()` \u53ea\u80fd\u7528\u65bc\u641c\u5c0b\u55ae\u4e00\u5b57\u5143\u6216\u5b50\u5b57\u4e32\u3002\u5982\u679c\u9700\u8981\u641c\u5c0b\u591a\u500b\u5b57\u5143\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u8ff4\u5708\u6216\u5176\u4ed6\u5b57\u4e32\u8655\u7406\u65b9\u6cd5\u3002<\/p>\n<p>**Q3: \u6709\u6c92\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u67e5\u627e\u5b57\u4e32\u4e2d\u7684\u5b57\u5143\uff1f**<br \/>\nA3: \u9664\u4e86 `index()` \u548c `find()`\uff0c\u4f60\u9084\u53ef\u4ee5\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u6216\u5176\u4ed6\u5b57\u4e32\u65b9\u6cd5\uff0c\u4f8b\u5982 `count()` \u4f86\u8a08\u7b97\u5b57\u5143\u51fa\u73fe\u6b21\u6578\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:index()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u5167\u5efa\u51fd\u6578\uff0c\u53ef\u4ee5\u7528\u4f86\u641c\u7d22\u5b57\u4e32\u4e2d\u6307\u5b9a\u5b57\u7b26\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u6307\u5b9a\u641c\u7d22\u7684\u8d77\u59cb\u4f4d\u7f6e\u548c\u7d50\u675f\u4f4d\u7f6e\uff0c\u4ee5\u53ca\u641c\u7d22\u7684\u5b57\u7b26\u4e32\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-3462","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-TQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3462","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=3462"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3462\/revisions"}],"predecessor-version":[{"id":12824,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3462\/revisions\/12824"}],"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=3462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}