{"id":1013284,"date":"2024-12-27T11:46:36","date_gmt":"2024-12-27T03:46:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1013284.html"},"modified":"2024-12-27T11:46:38","modified_gmt":"2024-12-27T03:46:38","slug":"python%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e5%88%97%e8%a1%a8%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1013284.html","title":{"rendered":"python\u5982\u4f55\u8fd4\u56de\u5217\u8868\u7d22\u5f15"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090750\/ef078d79-f8e1-4bf3-b31a-25cd9a789079.webp\" alt=\"python\u5982\u4f55\u8fd4\u56de\u5217\u8868\u7d22\u5f15\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8981\u8fd4\u56de\u5217\u8868\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528list.index()\u65b9\u6cd5\u3001\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662flist.index()\u65b9\u6cd5\u3001\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u548c\u6761\u4ef6\u5224\u65ad\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u7d22\u5f15\u67e5\u627e\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528list.index()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>list.index()\u65b9\u6cd5\u662fPython\u5185\u7f6e\u7684\u7528\u4e8e\u67e5\u627e\u5143\u7d20\u7d22\u5f15\u7684\u51fd\u6570\u3002\u5b83\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u67e5\u627e\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u5728\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff0c\u5219\u4f1a\u5f15\u53d1ValueError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<p>index = my_list.index(&#39;c&#39;)<\/p>\n<p>print(index)  # \u8f93\u51fa: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528list.index()\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\u5b83\u53ea\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u7d22\u5f15\uff0c\u5982\u679c\u9700\u8981\u67e5\u627e\u6240\u6709\u5339\u914d\u9879\u7684\u7d22\u5f15\uff0c\u5219\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5faa\u73af\u904d\u5386\u5217\u8868<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868\uff0c\u5e76\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5339\u914d\uff0c\u53ef\u4ee5\u624b\u52a8\u5b9e\u73b0\u7d22\u5f15\u67e5\u627e\u3002\u6b64\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u67e5\u627e\u5217\u8868\u4e2d\u6240\u6709\u5339\u914d\u9879\u7d22\u5f15\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;b&#39;, &#39;d&#39;]<\/p>\n<p>target = &#39;b&#39;<\/p>\n<p>indices = []<\/p>\n<p>for i, value in enumerate(my_list):<\/p>\n<p>    if value == target:<\/p>\n<p>        indices.append(i)<\/p>\n<p>print(indices)  # \u8f93\u51fa: [1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u8f83\u4e3a\u5197\u957f\uff0c\u4f46\u80fd\u7075\u6d3b\u5904\u7406\u590d\u6742\u7684\u67e5\u627e\u6761\u4ef6\uff0c\u5e76\u80fd\u8fd4\u56de\u591a\u4e2a\u5339\u914d\u9879\u7684\u7d22\u5f15\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5217\u8868\u3002\u5728\u67e5\u627e\u5143\u7d20\u7d22\u5f15\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408enumerate()\u51fd\u6570\uff0c\u5feb\u901f\u751f\u6210\u6240\u6709\u5339\u914d\u9879\u7684\u7d22\u5f15\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;b&#39;, &#39;d&#39;]<\/p>\n<p>target = &#39;b&#39;<\/p>\n<p>indices = [i for i, value in enumerate(my_list) if value == target]<\/p>\n<p>print(indices)  # \u8f93\u51fa: [1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u5143\u7d20\u7d22\u5f15\u67e5\u627e\uff0c\u5c24\u5176\u9002\u5408\u5bf9\u7b80\u5355\u6761\u4ef6\u7684\u5339\u914d\u8fdb\u884c\u67e5\u627e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528NumPy\u5e93\uff08\u9002\u7528\u4e8e\u6570\u503c\u5217\u8868\uff09<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u503c\u578b\u6570\u636e\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u67e5\u627e\u5143\u7d20\u7d22\u5f15\u3002NumPy\u7684where()\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_list = np.array([1, 2, 3, 2, 4])<\/p>\n<p>indices = np.where(my_list == 2)<\/p>\n<p>print(indices[0])  # \u8f93\u51fa: [1 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>NumPy\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\uff0c\u5176where()\u51fd\u6570\u80fd\u591f\u9ad8\u6548\u5730\u67e5\u627e\u6240\u6709\u5339\u914d\u9879\u7684\u7d22\u5f15\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528pandas\u5e93\uff08\u9002\u7528\u4e8e\u5904\u7406\u6570\u636e\u6846\uff09<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0cpandas\u5e93\u5e38\u7528\u4e8e\u5904\u7406\u6570\u636e\u6846\uff08DataFrame\uff09\u3002\u5982\u679c\u9700\u8981\u67e5\u627e\u67d0\u4e00\u5217\u4e2d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u5229\u7528pandas\u7684\u529f\u80fd\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;A&#39;: [&#39;foo&#39;, &#39;bar&#39;, &#39;baz&#39;, &#39;foo&#39;]})<\/p>\n<p>indices = df.index[df[&#39;A&#39;] == &#39;foo&#39;].tolist()<\/p>\n<p>print(indices)  # \u8f93\u51fa: [0, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>pandas\u5e93\u5728\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u6570\u636e\u5b58\u50a8\u5728\u6570\u636e\u6846\u4e2d\u65f6\uff0c\u53ef\u4ee5\u9ad8\u6548\u5b9e\u73b0\u7d22\u5f15\u67e5\u627e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u67e5\u627e\u5217\u8868\u5143\u7d20\u7d22\u5f15\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u4f7f\u7528\u573a\u666f\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u67e5\u627e\u4efb\u52a1\uff0clist.index()\u65b9\u6cd5\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u901a\u5e38\u8db3\u591f\uff1b\u800c\u5bf9\u4e8e\u9700\u8981\u67e5\u627e\u591a\u4e2a\u5339\u914d\u9879\u7684\u7d22\u5f15\u6216\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u5faa\u73af\u904d\u5386\u3001NumPy\u548cpandas\u7b49\u65b9\u6cd5\u5219\u66f4\u4e3a\u9002\u7528\u3002\u4e86\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u66f4\u7075\u6d3b\u5730\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u627e\u5230\u5217\u8868\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>list.index()<\/code>\u65b9\u6cd5\u6765\u67e5\u627e\u5143\u7d20\u5728\u5217\u8868\u4e2d\u7684\u7d22\u5f15\u3002\u5982\u679c\u5143\u7d20\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff0c\u8be5\u65b9\u6cd5\u5c06\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\u3002\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5c06\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [10, 20, 30, 40]\nindex = my_list.index(30)  # \u8fd4\u56de2\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\uff0c\u5982\u4f55\u83b7\u53d6\u6240\u6709\u8fd9\u4e9b\u5143\u7d20\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u8981\u83b7\u53d6\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u904d\u5386\u6574\u4e2a\u5217\u8868\uff0c\u627e\u51fa\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [10, 20, 30, 20, 40]\nindexes = [index for index, value in enumerate(my_list) if value == 20]  # \u8fd4\u56de[1, 3]\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u67e5\u627e\u7d22\u5f15\u65f6\u51fa\u73b0\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u67e5\u627e\u5143\u7d20\u7d22\u5f15\u65f6\uff0c\u5982\u679c\u8be5\u5143\u7d20\u4e0d\u5728\u5217\u8868\u4e2d\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230<code>ValueError<\/code>\u5f02\u5e38\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u63d0\u4f9b\u53cb\u597d\u7684\u63d0\u793a\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [10, 20, 30]\ntry:\n    index = my_list.index(40)\nexcept ValueError:\n    print(&quot;\u5143\u7d20\u4e0d\u5728\u5217\u8868\u4e2d\u3002&quot;)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8981\u8fd4\u56de\u5217\u8868\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528list.index()\u65b9\u6cd5\u3001\u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc [&hellip;]","protected":false},"author":3,"featured_media":1013290,"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\/1013284"}],"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=1013284"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013284\/revisions"}],"predecessor-version":[{"id":1013293,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013284\/revisions\/1013293"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1013290"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1013284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1013284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1013284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}