{"id":980010,"date":"2024-12-27T06:51:04","date_gmt":"2024-12-26T22:51:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/980010.html"},"modified":"2024-12-27T06:51:06","modified_gmt":"2024-12-26T22:51:06","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e5%88%97%e8%a1%a8%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/980010.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6\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\/24205605\/66d2c76f-55a9-40d8-858d-6dae963a7357.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6\u5217\u8868\u7d22\u5f15\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u63d0\u53d6\u5217\u8868\u7d22\u5f15\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u7d22\u5f15\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>enumerate()<\/code>\u627e\u5230\u6240\u6709\u5339\u914d\u7d22\u5f15\u3001\u901a\u8fc7\u5faa\u73af\u624b\u52a8\u641c\u7d22\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\uff0c\u800c\u5bf9\u4e8e\u9700\u8981\u83b7\u53d6\u591a\u4e2a\u5339\u914d\u7d22\u5f15\u7684\u60c5\u51b5\uff0c\u7ed3\u5408<code>enumerate()<\/code>\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u4f1a\u66f4\u4e3a\u5408\u9002\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>index()<\/code>\u65b9\u6cd5\u662fPython\u4e2d\u5185\u7f6e\u7684\u5217\u8868\u65b9\u6cd5\uff0c\u7528\u4e8e\u67e5\u627e\u7279\u5b9a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u7d22\u5f15\u3002\u5f53\u53ea\u9700\u8981\u77e5\u9053\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u51fa\u73b0\u4f4d\u7f6e\u65f6\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\u3002\u7136\u800c\uff0c\u5982\u679c\u5143\u7d20\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u591a\u6b21\uff0c\u8be5\u65b9\u6cd5\u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;banana&#39;]<\/p>\n<p>index_of_banana = my_list.index(&#39;banana&#39;)<\/p>\n<p>print(index_of_banana)  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u4e0d\u5b58\u5728\u8be5\u5143\u7d20\uff0c<code>index()<\/code>\u65b9\u6cd5\u5c06\u629b\u51fa<code>ValueError<\/code>\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u6b64\u65b9\u6cd5\u65f6\uff0c\u6700\u597d\u5728\u8c03\u7528\u4e4b\u524d\u786e\u8ba4\u5143\u7d20\u786e\u5b9e\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>enumerate()<\/code><\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u83b7\u53d6\u5217\u8868\u4e2d\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\u65f6\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>enumerate()<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<code>enumerate()<\/code>\u51fd\u6570\u4e3a\u5217\u8868\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7d22\u5f15\u548c\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u540c\u65f6\u8bbf\u95ee\u5143\u7d20\u53ca\u5176\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;banana&#39;]<\/p>\n<p>indices_of_banana = [index for index, value in enumerate(my_list) if value == &#39;banana&#39;]<\/p>\n<p>print(indices_of_banana)  # \u8f93\u51fa\uff1a[1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\u4e5f\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u5bf9\u5217\u8868\u7684\u591a\u6b21\u904d\u5386\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5faa\u73af\u624b\u52a8\u641c\u7d22<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u67d0\u4e9b\u590d\u6742\u60c5\u51b5\uff0c\u4f8b\u5982\u9700\u8981\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\uff0c\u4f7f\u7528\u5faa\u73af\u624b\u52a8\u641c\u7d22\u53ef\u80fd\u662f\u4e00\u4e2a\u66f4\u7075\u6d3b\u7684\u9009\u62e9\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u5728\u627e\u5230\u5339\u914d\u65f6\u8bb0\u5f55\u5176\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;banana&#39;]<\/p>\n<p>indices_of_banana = []<\/p>\n<p>for index, value in enumerate(my_list):<\/p>\n<p>    if value == &#39;banana&#39;:<\/p>\n<p>        indices_of_banana.append(index)<\/p>\n<p>print(indices_of_banana)  # \u8f93\u51fa\uff1a[1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4ee3\u7801\u91cf\u7a0d\u591a\uff0c\u4f46\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u6267\u884c\u66f4\u591a\u7684\u64cd\u4f5c\uff0c\u9002\u5408\u9700\u8981\u590d\u6742\u6761\u4ef6\u5224\u65ad\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93\u8fdb\u884c\u7d22\u5f15\u63d0\u53d6<\/p>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u662f\u7531\u6570\u503c\u7ec4\u6210\u7684\uff0c\u5e76\u4e14\u60a8\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7d22\u5f15\u64cd\u4f5c\uff0c<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a<code>numpy<\/code>\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528<code>numpy<\/code>\u7684\u77e2\u91cf\u5316\u64cd\u4f5c\u6765\u5feb\u901f\u83b7\u53d6\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_list = [10, 20, 30, 20, 10]<\/p>\n<p>array = np.array(my_list)<\/p>\n<p>indices_of_20 = np.where(array == 20)[0]<\/p>\n<p>print(indices_of_20)  # \u8f93\u51fa\uff1a[1 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>numpy<\/code>\u7684<code>where<\/code>\u51fd\u6570\u8fd4\u56de\u6ee1\u8db3\u6761\u4ef6\u7684\u6240\u6709\u7d22\u5f15\uff0c\u5bf9\u4e8e\u6570\u503c\u8ba1\u7b97\u548c\u79d1\u5b66\u8fd0\u7b97\u6765\u8bf4\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528Pandas\u5e93\u8fdb\u884c\u7d22\u5f15\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0cPandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002\u867d\u7136\u5b83\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\uff0c\u4f46\u5728\u5904\u7406\u4e00\u7ef4\u6570\u636e\u65f6\u4e5f\u80fd\u6d3e\u4e0a\u7528\u573a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;banana&#39;]<\/p>\n<p>series = pd.Series(my_list)<\/p>\n<p>indices_of_banana = series[series == &#39;banana&#39;].index.tolist()<\/p>\n<p>print(indices_of_banana)  # \u8f93\u51fa\uff1a[1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pandas\u7684<code>Series<\/code>\u5bf9\u8c61\u4e0d\u4ec5\u652f\u6301\u6761\u4ef6\u7b5b\u9009\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5de5\u5177\uff0c\u975e\u5e38\u9002\u5408\u5728\u6570\u636e\u79d1\u5b66\u9886\u57df\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u63d0\u53d6\u5217\u8868\u7d22\u5f15\uff0c\u4e0d\u540c\u7684\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5217\u8868\u7684\u7279\u5b9a\u5143\u7d20\u7d22\u5f15\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list.index(value)<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6307\u5b9a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [10, 20, 30, 40]<\/code>\uff0c\u8981\u83b7\u53d6<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>\u3002\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u6ca1\u6709\u8be5\u5143\u7d20\uff0cPython\u4f1a\u629b\u51fa<code>ValueError<\/code>\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u63d0\u53d6\u6240\u6709\u5143\u7d20\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>enumerate()<\/code>\u51fd\u6570\u7ed3\u5408\u5217\u8868\u89e3\u6790\u6765\u63d0\u53d6\u6240\u6709\u5143\u7d20\u7684\u7d22\u5f15\u3002<code>enumerate()<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u751f\u6210\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>[(index, value) for index, value in enumerate(my_list)]<\/code>\uff0c\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6bcf\u4e2a\u5143\u7d20\u53ca\u5176\u7d22\u5f15\u7684\u5217\u8868\uff0c\u5982<code>[(0, 10), (1, 20), (2, 30), (3, 40)]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u91cd\u590d\u5143\u7d20\u7684\u7d22\u5f15\u63d0\u53d6\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u63d0\u53d6\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u5217\u8868<code>my_list = [10, 20, 10, 30]<\/code>\uff0c\u60f3\u8981\u627e\u5230\u6240\u6709<code>10<\/code>\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528<code>[i for i, x in enumerate(my_list) if x == 10]<\/code>\uff0c\u8fd9\u4f1a\u8fd4\u56de<code>[0, 2]<\/code>\uff0c\u663e\u793a<code>10<\/code>\u5728\u5217\u8868\u4e2d\u7684\u6240\u6709\u4f4d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u63d0\u53d6\u5217\u8868\u7d22\u5f15\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528index()\u65b9\u6cd5\u83b7\u53d6\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u7d22\u5f15\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408e [&hellip;]","protected":false},"author":3,"featured_media":980017,"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\/980010"}],"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=980010"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980010\/revisions"}],"predecessor-version":[{"id":980018,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980010\/revisions\/980018"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/980017"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=980010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=980010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=980010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}