{"id":981728,"date":"2024-12-27T07:05:00","date_gmt":"2024-12-26T23:05:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981728.html"},"modified":"2024-12-27T07:05:04","modified_gmt":"2024-12-26T23:05:04","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e5%85%83%e7%b4%a0%e4%b8%8b%e6%a0%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981728.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6\u5143\u7d20\u4e0b\u6807"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210538\/d3ab4be0-b59f-4a15-8fbd-d776ae387bf2.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6\u5143\u7d20\u4e0b\u6807\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u63d0\u53d6\u5143\u7d20\u4e0b\u6807\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684index()\u65b9\u6cd5\u3001enumerate()\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u5f0f\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528index()\u65b9\u6cd5\u548cenumerate()\u51fd\u6570\u3002\u4f7f\u7528index()\u65b9\u6cd5\u65f6\uff0c\u8981\u6ce8\u610f\u5143\u7d20\u5728\u5217\u8868\u4e2d\u662f\u5426\u5b58\u5728\uff0c\u56e0\u4e3a\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u53ca\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528INDEX()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528index()\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u5217\u8868\u4e2d\u6307\u5b9a\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u7d22\u5f15\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u82e5\u5143\u7d20\u4e0d\u5728\u5217\u8868\u4e2d\uff0c\u5219\u4f1a\u629b\u51faValueError\u5f02\u5e38\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u901a\u5e38\u9700\u8981\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [10, 20, 30, 40, 30]<\/p>\n<p>try:<\/p>\n<p>    index = my_list.index(30)<\/p>\n<p>    print(f&quot;\u5143\u7d2030\u7684\u7b2c\u4e00\u4e2a\u4e0b\u6807\u662f: {index}&quot;)<\/p>\n<p>except ValueError:<\/p>\n<p>    print(&quot;\u5143\u7d20\u4e0d\u5728\u5217\u8868\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>index()\u65b9\u6cd5\u9002\u7528\u4e8e\u5f53\u6211\u4eec\u786e\u5b9a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u5b58\u5728\u65f6\uff0c\u5e76\u4e14\u6211\u4eec\u53ea\u9700\u8981\u627e\u5230\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u4e0b\u6807\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)\uff0c\u56e0\u4e3a\u5728\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u5fc5\u987b\u904d\u5386\u6574\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528ENUMERATE()\u51fd\u6570<\/p>\n<\/p>\n<p><p>enumerate()\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u540c\u65f6\u83b7\u5f97\u5143\u7d20\u7d22\u5f15\u548c\u503c\u7684\u7b80\u4fbf\u65b9\u6cd5\u3002\u5728\u9700\u8981\u904d\u5386\u6574\u4e2a\u5217\u8868\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u65f6\uff0c\u4f7f\u7528enumerate()\u51fd\u6570\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [10, 20, 30, 40, 30]<\/p>\n<p>target = 30<\/p>\n<p>indices = [index for index, value in enumerate(my_list) if value == target]<\/p>\n<p>print(f&quot;\u5143\u7d2030\u7684\u6240\u6709\u4e0b\u6807\u662f: {indices}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528enumerate()\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u627e\u5230\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u7279\u522b\u662f\u5f53\u5143\u7d20\u53ef\u80fd\u591a\u6b21\u51fa\u73b0\u65f6\u3002\u5176\u4f18\u52bf\u5728\u4e8e\u904d\u5386\u65f6\u4e0d\u4ec5\u83b7\u5f97\u5143\u7d20\u503c\uff0c\u8fd8\u53ef\u4ee5\u83b7\u53d6\u5176\u5bf9\u5e94\u7684\u7d22\u5f15\u3002<\/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\u975e\u5e38Pythonic\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u8fdb\u884c\u590d\u6742\u7684\u64cd\u4f5c\u3002\u7ed3\u5408enumerate()\uff0c\u53ef\u4ee5\u5feb\u901f\u63d0\u53d6\u51fa\u76ee\u6807\u5143\u7d20\u7684\u6240\u6709\u4e0b\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>my_list = [10, 20, 30, 40, 30]<\/p>\n<p>target = 30<\/p>\n<p>indices = [i for i, x in enumerate(my_list) if x == target]<\/p>\n<p>print(f&quot;\u5143\u7d2030\u7684\u6240\u6709\u4e0b\u6807\u662f: {indices}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u4ee3\u7801\u7b80\u6d01\uff0c\u9002\u5408\u5728\u4ee3\u7801\u4e2d\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u4f7f\u7528\u3002\u4f46\u9700\u8981\u6ce8\u610f\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u53ef\u8bfb\u6027\uff0c\u5c24\u5176\u662f\u5728\u6d89\u53ca\u590d\u6742\u903b\u8f91\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528NUMPY\u5e93<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u6216\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7684\u573a\u666f\uff0c\u901a\u5e38\u4f7f\u7528NumPy\u5e93\u6765\u63d0\u9ad8\u6548\u7387\u3002NumPy\u63d0\u4f9b\u4e86\u66f4\u5feb\u7684\u6570\u7ec4\u64cd\u4f5c\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/h2>\n<p>my_array = np.array([10, 20, 30, 40, 30])<\/p>\n<p>indices = np.where(my_array == 30)[0]<\/p>\n<p>print(f&quot;\u5143\u7d2030\u7684\u6240\u6709\u4e0b\u6807\u662f: {indices}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u7684where()\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u7ec4\u548c\u77e9\u9635\u65f6\u5bfb\u627e\u7279\u5b9a\u5143\u7d20\u7684\u7d22\u5f15\u3002\u8fd9\u662f\u56e0\u4e3aNumPy\u5e95\u5c42\u662f\u7528C\u5b9e\u73b0\u7684\uff0c\u5177\u6709\u66f4\u5feb\u7684\u8ba1\u7b97\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\u3002\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u9700\u6c42\u5b9e\u73b0\u5bf9\u5217\u8868\u7684\u641c\u7d22\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>def find_indices(lst, value):<\/p>\n<p>    return [i for i, x in enumerate(lst) if x == value]<\/p>\n<p>my_list = [10, 20, 30, 40, 30]<\/p>\n<p>indices = find_indices(my_list, 30)<\/p>\n<p>print(f&quot;\u5143\u7d2030\u7684\u6240\u6709\u4e0b\u6807\u662f: {indices}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u6269\u5c55\uff0c\u4f8b\u5982\u5728\u641c\u7d22\u8fc7\u7a0b\u4e2d\u52a0\u5165\u66f4\u591a\u7684\u903b\u8f91\u5224\u65ad\u6216\u5904\u7406\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u5728\u4e0d\u540c\u573a\u5408\u91cd\u590d\u4f7f\u7528\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u63d0\u53d6\u5143\u7d20\u4e0b\u6807\u53ef\u4ee5\u6709\u591a\u79cd\u65b9\u6cd5\uff0c<strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u89c4\u6a21<\/strong>\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5e94\u7528\u573a\u666f\uff0c\u4f7f\u7528\u5185\u7f6e\u7684index()\u65b9\u6cd5\u548cenumerate()\u51fd\u6570\u901a\u5e38\u8db3\u591f\uff1b\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7684\u573a\u5408\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\uff1b\u800c\u5bf9\u4e8e\u9700\u8981\u7075\u6d3b\u5904\u7406\u7684\u573a\u666f\uff0c\u81ea\u5b9a\u4e49\u51fd\u6570\u5219\u63d0\u4f9b\u4e86\u6700\u5927\u7684\u81ea\u7531\u5ea6\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u6ce8\u91cd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\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\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u4e0b\u6807\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list.index()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u67d0\u4e2a\u5143\u7d20\u7684\u4e0b\u6807\u3002\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u67e5\u627e\u7684\u5143\u7d20\uff0c\u5982\u679c\u8be5\u5143\u7d20\u5728\u5217\u8868\u4e2d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u5b83\u7684\u4e0b\u6807\u3002\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [10, 20, 30, 40]\nindex = my_list.index(30)  # index\u5c06\u4f1a\u662f2\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u5217\u8868\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u4e0b\u6807\u63d0\u53d6\uff1f<\/strong><br \/>\u5f53\u5217\u8868\u4e2d\u5b58\u5728\u91cd\u590d\u5143\u7d20\u65f6\uff0c<code>list.index()<\/code>\u65b9\u6cd5\u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\u7684\u4e0b\u6807\u3002\u82e5\u60f3\u83b7\u53d6\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u4e0b\u6807\uff0c\u53ef\u4ee5\u7ed3\u5408<code>enumerate()<\/code>\u51fd\u6570\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u63d0\u53d6\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u4e0b\u6807\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [10, 20, 30, 20, 40]\nindices = [index for index, value in enumerate(my_list) if value == 20]  # indices\u5c06\u662f[1, 3]\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u6765\u5224\u65ad\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5728\u5217\u8868\u4e2d\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u8fd4\u56de\u5e03\u5c14\u503c\u3002\u5982\u679c\u9700\u8981\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u7684\u4e0b\u6807\uff0c\u53ef\u4ee5\u7ed3\u5408<code>if<\/code>\u8bed\u53e5\u548c<code>list.index()<\/code>\u65b9\u6cd5\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [10, 20, 30, 40]\nif 30 in my_list:\n    index = my_list.index(30)  # index\u5c06\u4f1a\u662f2\nelse:\n    index = -1  # \u8868\u793a\u5143\u7d20\u4e0d\u5b58\u5728\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u63d0\u53d6\u5143\u7d20\u4e0b\u6807\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684index()\u65b9\u6cd5\u3001enumerate()\u51fd\u6570\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u5f0f\uff0c [&hellip;]","protected":false},"author":3,"featured_media":981735,"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\/981728"}],"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=981728"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981728\/revisions"}],"predecessor-version":[{"id":981736,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981728\/revisions\/981736"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/981735"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=981728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=981728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=981728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}