{"id":1064246,"date":"2024-12-31T16:07:22","date_gmt":"2024-12-31T08:07:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1064246.html"},"modified":"2024-12-31T16:07:25","modified_gmt":"2024-12-31T08:07:25","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96python%e5%88%97%e8%a1%a8%e4%b8%ad%e5%85%83%e7%b4%a0%e7%9a%84%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1064246.html","title":{"rendered":"\u5982\u4f55\u83b7\u53d6Python\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7d22\u5f15"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/72a4e280-db8a-44ba-b677-e2aa07b69301.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u83b7\u53d6Python\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7d22\u5f15\" \/><\/p>\n<p><p> <strong>\u8981\u83b7\u53d6Python\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u901a\u8fc7\u8fed\u4ee3\u67e5\u627e\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4f1a\u8fd4\u56de\u5217\u8868\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u6307\u5b9a\u5143\u7d20\u7684\u7d22\u5f15\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u6765\u67e5\u627e\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><p><code>index()<\/code>\u65b9\u6cd5\u662f\u4e00\u79cd\u5185\u7f6e\u65b9\u6cd5\uff0c\u4e13\u95e8\u7528\u4e8e\u67e5\u627e\u5217\u8868\u4e2d\u67d0\u4e2a\u5143\u7d20\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u3002\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8c03\u7528\u8be5\u65b9\u6cd5\u5e76\u4f20\u5165\u6240\u67e5\u627e\u7684\u5143\u7d20\u5373\u53ef\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u4e0d\u5b58\u5728\u6240\u67e5\u627e\u7684\u5143\u7d20\uff0c\u8be5\u65b9\u6cd5\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>index()<\/code>\u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u67e5\u627e\u5217\u8868\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list.index(element, start, end)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>element<\/code>\uff1a\u8981\u67e5\u627e\u7684\u5143\u7d20\u3002<\/li>\n<li><code>start<\/code>\uff1a\u53ef\u9009\uff0c\u6307\u5b9a\u641c\u7d22\u7684\u8d77\u59cb\u4f4d\u7f6e\uff08\u7d22\u5f15\uff09\u3002<\/li>\n<li><code>end<\/code>\uff1a\u53ef\u9009\uff0c\u6307\u5b9a\u641c\u7d22\u7684\u7ed3\u675f\u4f4d\u7f6e\uff08\u7d22\u5f15\uff09\u3002<\/li>\n<\/ul>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>index = my_list.index(30)  # \u8fd4\u56de 2<\/p>\n<p>print(f&quot;Element 30 is at index: {index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u67e5\u627e\u5143\u7d20<code>30<\/code>\u7684\u7d22\u5f15\uff0c\u7ed3\u679c\u662f<code>2<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u5217\u8868\u4e2d\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">indices = [i for i, x in enumerate(my_list) if x == element]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>enumerate(my_list)<\/code>\uff1a\u8fd4\u56de\u5305\u542b\u7d22\u5f15\u548c\u503c\u7684\u5143\u7ec4\u3002<\/li>\n<li><code>if x == element<\/code>\uff1a\u6761\u4ef6\u5224\u65ad\uff0c\u7b5b\u9009\u51fa\u5339\u914d\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 30, 40, 50]<\/p>\n<p>indices = [i for i, x in enumerate(my_list) if x == 30]  # \u8fd4\u56de [2, 3]<\/p>\n<p>print(f&quot;Element 30 is at indices: {indices}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u67e5\u627e\u6240\u6709\u503c\u4e3a<code>30<\/code>\u7684\u5143\u7d20\u7d22\u5f15\uff0c\u7ed3\u679c\u662f<code>[2, 3]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u8fed\u4ee3\u67e5\u627e<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u904d\u5386\u5217\u8868\uff0c\u53ef\u4ee5\u624b\u52a8\u67e5\u627e\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u67e5\u627e\u6761\u4ef6\uff0c\u4f8b\u5982\u67e5\u627e\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>for i, value in enumerate(my_list):<\/p>\n<p>    if value == 30:<\/p>\n<p>        print(f&quot;Element 30 is at index: {i}&quot;)<\/p>\n<p>        break  # \u627e\u5230\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u540e\u505c\u6b62\u67e5\u627e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u904d\u5386\u5217\u8868\u67e5\u627e\u5143\u7d20<code>30<\/code>\u7684\u7d22\u5f15\uff0c\u5e76\u5728\u627e\u5230\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u540e\u505c\u6b62\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u6761\u4ef6\u67e5\u627e<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u67e5\u627e\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5076\u6570\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 21, 32, 43, 54]<\/p>\n<p>index = next((i for i, x in enumerate(my_list) if x % 2 == 0), None)<\/p>\n<p>print(f&quot;The first even number is at index: {index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u67e5\u627e\u5217\u8868\u4e2d\u7b2c\u4e00\u4e2a\u5076\u6570\u7684\u7d22\u5f15\uff0c\u7ed3\u679c\u662f<code>0<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u3001<code>except<\/code>\u7ed3\u6784\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>try:<\/p>\n<p>    index = my_list.index(60)<\/p>\n<p>    print(f&quot;Element 60 is at index: {index}&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;Element 60 is not in the list&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u67e5\u627e\u5143\u7d20<code>60<\/code>\u7684\u7d22\u5f15\uff0c\u7531\u4e8e<code>60<\/code>\u4e0d\u5728\u5217\u8868\u4e2d\uff0c\u56e0\u6b64\u6355\u83b7\u5230<code>ValueError<\/code>\u5f02\u5e38\uff0c\u5e76\u8f93\u51fa\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u83b7\u53d6\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u5217\u8868\u4e2d\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8fed\u4ee3\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 30, 40, 50]<\/p>\n<p>indices = [i for i, x in enumerate(my_list) if x == 30]<\/p>\n<p>print(f&quot;All indices of element 30: {indices}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u83b7\u53d6\u5217\u8868\u4e2d\u6240\u6709\u503c\u4e3a<code>30<\/code>\u7684\u5143\u7d20\u7d22\u5f15\uff0c\u7ed3\u679c\u662f<code>[2, 3]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5728\u6307\u5b9a\u8303\u56f4\u5185\u67e5\u627e<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u53ea\u9700\u8981\u5728\u5217\u8868\u7684\u6307\u5b9a\u8303\u56f4\u5185\u67e5\u627e\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u7684<code>start<\/code>\u548c<code>end<\/code>\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50, 30, 60]<\/p>\n<p>index = my_list.index(30, 3, 6)  # \u67e5\u627e\u4ece\u7d22\u5f153\u5230\u7d22\u5f156\u4e4b\u95f4\u768430<\/p>\n<p>print(f&quot;Element 30 is at index: {index}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u7d22\u5f15<code>3<\/code>\u5230\u7d22\u5f15<code>6<\/code>\u4e4b\u95f4\u67e5\u627e\u5143\u7d20<code>30<\/code>\u7684\u7d22\u5f15\uff0c\u7ed3\u679c\u662f<code>5<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u67e5\u627e\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u5143\u7d20\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5d4c\u5957\u5217\u8868\uff0c\u83b7\u53d6\u5143\u7d20\u7684\u7d22\u5f15\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002\u9700\u8981\u904d\u5386\u5916\u5c42\u548c\u5185\u5c42\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [[10, 20], [30, 40], [50, 60]]<\/p>\n<p>for i, sublist in enumerate(my_list):<\/p>\n<p>    try:<\/p>\n<p>        index = sublist.index(30)<\/p>\n<p>        print(f&quot;Element 30 is at index: ({i}, {index})&quot;)<\/p>\n<p>        break<\/p>\n<p>    except ValueError:<\/p>\n<p>        continue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u67e5\u627e\u5d4c\u5957\u5217\u8868\u4e2d\u5143\u7d20<code>30<\/code>\u7684\u7d22\u5f15\uff0c\u7ed3\u679c\u662f<code>(1, 0)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u67e5\u627e\u591a\u4e2a\u6761\u4ef6\u7684\u5143\u7d20\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u67e5\u627e\u6ee1\u8db3\u591a\u4e2a\u6761\u4ef6\u7684\u5143\u7d20\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u5217\u8868\u4e2d\u6240\u6709\u5076\u6570\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 21, 32, 43, 54]<\/p>\n<p>indices = [i for i, x in enumerate(my_list) if x % 2 == 0]<\/p>\n<p>print(f&quot;Indices of even numbers: {indices}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u67e5\u627e\u5217\u8868\u4e2d\u6240\u6709\u5076\u6570\u7684\u7d22\u5f15\uff0c\u7ed3\u679c\u662f<code>[0, 2, 4]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6Python\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7d22\u5f15\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u901a\u8fc7\u8fed\u4ee3\u67e5\u627e\u7b49\u3002<strong><code>index()<\/code>\u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u7528\u4e8e\u67e5\u627e\u5355\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u67e5\u627e\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u901a\u8fc7\u8fed\u4ee3\u5b9e\u73b0\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u67e5\u627e\u3001\u5728\u6307\u5b9a\u8303\u56f4\u5185\u67e5\u627e\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u7b49\u3002\u4e86\u89e3\u5e76\u719f\u7ec3\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347Python\u7f16\u7a0b\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u7279\u5b9a\u5143\u7d20\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684<code>index()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u7279\u5b9a\u5143\u7d20\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [10, 20, 30, 40]<\/code>\uff0c\u60f3\u8981\u627e\u5230\u5143\u7d20<code>30<\/code>\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_list.index(30)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>2<\/code>\uff0c\u56e0\u4e3a<code>30<\/code>\u5728\u5217\u8868\u4e2d\u7684\u4f4d\u7f6e\u662f\u7d22\u5f152\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u5143\u7d20\uff0c\u6211\u8be5\u5982\u4f55\u83b7\u53d6\u6240\u6709\u7d22\u5f15\uff1f<\/strong><br \/>\u5f53\u5217\u8868\u4e2d\u5b58\u5728\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\u65f6\uff0c<code>index()<\/code>\u65b9\u6cd5\u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u7d22\u5f15\u3002\u4e3a\u4e86\u83b7\u53d6\u6240\u6709\u76f8\u540c\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7684\u5217\u8868\u662f<code>my_list = [10, 20, 30, 20, 40]<\/code>\uff0c\u60f3\u8981\u627e\u5230\u5143\u7d20<code>20<\/code>\u7684\u6240\u6709\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<code>[index for index, value in enumerate(my_list) if value == 20]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>[1, 3]<\/code>\uff0c\u8868\u793a<code>20<\/code>\u5728\u5217\u8868\u4e2d\u7684\u4e24\u4e2a\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u83b7\u53d6\u7d22\u5f15\u65f6\u5982\u4f55\u5904\u7406\u5143\u7d20\u4e0d\u5728\u5217\u8868\u4e2d\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5c1d\u8bd5\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u67e5\u627e\u4e00\u4e2a\u4e0d\u5728\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5148\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c<code>if 50 in my_list:<\/code>\u53ef\u4ee5\u5224\u65ad<code>50<\/code>\u662f\u5426\u5728<code>my_list<\/code>\u4e2d\u3002\u5982\u679c\u5b58\u5728\uff0c\u518d\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u83b7\u53d6\u7d22\u5f15\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u83b7\u53d6Python\u5217\u8868\u4e2d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528index()\u65b9\u6cd5\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u901a\u8fc7\u8fed\u4ee3\u67e5\u627e [&hellip;]","protected":false},"author":3,"featured_media":1064250,"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\/1064246"}],"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=1064246"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064246\/revisions"}],"predecessor-version":[{"id":1064254,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064246\/revisions\/1064254"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1064250"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1064246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1064246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1064246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}