{"id":988000,"date":"2024-12-27T07:58:26","date_gmt":"2024-12-26T23:58:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/988000.html"},"modified":"2024-12-27T07:58:28","modified_gmt":"2024-12-26T23:58:28","slug":"python%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e6%95%b0%e7%bb%84%e4%b8%8b%e6%a0%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/988000.html","title":{"rendered":"python\u5982\u4f55\u8fd4\u56de\u6570\u7ec4\u4e0b\u6807"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063851\/05c0daf1-ab83-4c88-889e-2f3144635400.webp\" alt=\"python\u5982\u4f55\u8fd4\u56de\u6570\u7ec4\u4e0b\u6807\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u8fd4\u56de\u6570\u7ec4\uff08\u6216\u5217\u8868\uff09\u4e2d\u5143\u7d20\u7684\u4e0b\u6807\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<strong>\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u3001\u679a\u4e3e\uff08<code>enumerate<\/code>\uff09\u3001\u5faa\u73af\u904d\u5386\u7b49\u662f\u83b7\u53d6\u5217\u8868\u4e2d\u5143\u7d20\u4e0b\u6807\u7684\u5e38\u7528\u65b9\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c<code>index()<\/code>\u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u4f46\u662f\u5b83\u53ea\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\u7684\u4e0b\u6807\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528INDEX()\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>index()<\/code>\u65b9\u6cd5\u662fPython\u5185\u7f6e\u7684\u5217\u8868\u65b9\u6cd5\uff0c\u7528\u4e8e\u67e5\u627e\u5143\u7d20\u5728\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u4e0b\u6807\u3002\u5982\u679c\u5217\u8868\u4e2d\u6ca1\u6709\u8be5\u5143\u7d20\uff0c<code>index()<\/code>\u65b9\u6cd5\u5c06\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u8c03\u7528\u8be5\u65b9\u6cd5\u4e4b\u524d\uff0c\u6700\u597d\u786e\u8ba4\u8be5\u5143\u7d20\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>index = my_list.index(30)<\/p>\n<p>print(index)  # \u8f93\u51fa: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>index()<\/code>\u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u5143\u7d20<code>30<\/code>\u5728\u5217\u8868\u4e2d\u7684\u4e0b\u6807\uff0c\u8fd4\u56de\u7ed3\u679c\u4e3a<code>2<\/code>\u3002<strong><code>index()<\/code>\u65b9\u6cd5\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u7b80\u6d01\u6613\u7528\uff0c\u9002\u5408\u67e5\u627e\u5217\u8868\u4e2d\u5b58\u5728\u7684\u552f\u4e00\u6216\u7b2c\u4e00\u4e2a\u5143\u7d20<\/strong>\u3002\u7136\u800c\uff0c\u5982\u679c\u9700\u8981\u67e5\u627e\u591a\u4e2a\u76f8\u540c\u5143\u7d20\u7684\u4e0b\u6807\uff0c<code>index()<\/code>\u65b9\u6cd5\u5e76\u4e0d\u9002\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u679a\u4e3e\uff08ENUMERATE\uff09<\/p>\n<\/p>\n<p><p><code>enumerate()<\/code>\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u904d\u5386\u5217\u8868\u7684\u5143\u7d20\u548c\u4e0b\u6807\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u4e0b\u6807\u548c\u5143\u7d20\u7684\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\u3002\u4f7f\u7528<code>enumerate()<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u627e\u5230\u6240\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u7684\u4e0b\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 30, 50]<\/p>\n<p>indices = [index for index, value in enumerate(my_list) if value == 30]<\/p>\n<p>print(indices)  # \u8f93\u51fa: [2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>enumerate()<\/code>\u51fd\u6570\u904d\u5386\u5217\u8868\uff0c\u67e5\u627e\u503c\u4e3a<code>30<\/code>\u7684\u6240\u6709\u4e0b\u6807\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728<code>indices<\/code>\u5217\u8868\u4e2d\u3002<strong>\u4f7f\u7528<code>enumerate()<\/code>\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u627e\u5230\u591a\u4e2a\u76f8\u540c\u5143\u7d20\u7684\u4e0b\u6807\uff0c\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u5730\u8bbe\u7f6e\u67e5\u627e\u6761\u4ef6<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0d\u719f\u6089Python\u5185\u7f6e\u51fd\u6570\u7684\u4eba\u6765\u8bf4\uff0c\u5faa\u73af\u904d\u5386\u5217\u8868\u4e5f\u662f\u4e00\u4e2a\u67e5\u627e\u5143\u7d20\u4e0b\u6807\u7684\u5e38\u7528\u65b9\u6cd5\u3002\u901a\u8fc7\u624b\u52a8\u904d\u5386\u5217\u8868\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u67e5\u627e\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>indices = []<\/p>\n<p>for i in range(len(my_list)):<\/p>\n<p>    if my_list[i] == 30:<\/p>\n<p>        indices.append(i)<\/p>\n<p>print(indices)  # \u8f93\u51fa: [2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u548c\u5b83\u7684\u4e0b\u6807\uff0c\u67e5\u627e\u503c\u4e3a<code>30<\/code>\u7684\u5143\u7d20\u5e76\u8bb0\u5f55\u5b83\u4eec\u7684\u4e0b\u6807\u3002<strong>\u4f7f\u7528\u5faa\u73af\u904d\u5386\u7684\u4f18\u70b9\u662f\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9e\u73b0\u590d\u6742\u7684\u67e5\u627e\u903b\u8f91<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528NUMPY\u5e93<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u7684\u64cd\u4f5c\uff0c<code>NumPy<\/code>\u5e93\u63d0\u4f9b\u4e86\u66f4\u52a0\u9ad8\u6548\u7684\u65b9\u6cd5\u3002<code>np.where()<\/code>\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u6570\u7ec4\u4e2d\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u7684\u4e0b\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.array([10, 20, 30, 30, 50])<\/p>\n<p>indices = np.where(my_array == 30)<\/p>\n<p>print(indices[0])  # \u8f93\u51fa: [2 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>np.where()<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u4e0b\u6807\u3002<strong><code>NumPy<\/code>\u5e93\u7684\u4f18\u70b9\u662f\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u901f\u5ea6\u5feb\uff0c\u9002\u5408\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u67e5\u627e\u6570\u7ec4\u6216\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4e0b\u6807\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\u3002<strong><code>index()<\/code>\u65b9\u6cd5\u9002\u5408\u67e5\u627e\u5355\u4e2a\u5143\u7d20\u7684\u4e0b\u6807\uff0c<code>enumerate()<\/code>\u548c\u5faa\u73af\u904d\u5386\u9002\u5408\u67e5\u627e\u591a\u4e2a\u76f8\u540c\u5143\u7d20\u7684\u4e0b\u6807\uff0c<code>NumPy<\/code>\u5e93\u5219\u63d0\u4f9b\u4e86\u9ad8\u6548\u5904\u7406\u6570\u7ec4\u7684\u65b9\u6848<\/strong>\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\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\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4e0b\u6807\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list.index()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6570\u7ec4\uff08\u5217\u8868\uff09\u4e2d\u67d0\u4e2a\u5143\u7d20\u7684\u4e0b\u6807\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [10, 20, 30, 40]<\/code>\uff0c\u8981\u627e\u5230\u5143\u7d20<code>30<\/code>\u7684\u4e0b\u6807\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\u7b2c\u4e8c\u4e2a\uff08\u4ece0\u5f00\u59cb\u8ba1\u6570\uff09\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5217\u8868\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u4e0b\u6807\uff1f<\/strong><br \/>\u5982\u679c\u5217\u8868\u4e2d\u6709\u91cd\u590d\u7684\u5143\u7d20\uff0c<code>list.index()<\/code>\u65b9\u6cd5\u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u4e0b\u6807\u3002\u5982\u679c\u9700\u8981\u627e\u5230\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u4e0b\u6807\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>enumerate()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5217\u8868<code>my_list = [10, 20, 30, 20, 40]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>[index for index, value in enumerate(my_list) if value == 20]<\/code>\u6765\u83b7\u53d6\u6240\u6709<code>20<\/code>\u7684\u4e0b\u6807\uff0c\u7ed3\u679c\u5c06\u662f<code>[1, 3]<\/code>\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6570\u7ec4\u5143\u7d20\u7684\u4e0b\u6807\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>list.index()<\/code>\u65b9\u6cd5\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5904\u7406\u6570\u7ec4\u3002\u4f7f\u7528NumPy\u65f6\uff0c<code>numpy.where()<\/code>\u51fd\u6570\u80fd\u591f\u627e\u5230\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u4e0b\u6807\u3002\u4f8b\u5982\uff0c<code>import numpy as np<\/code>\u540e\uff0c\u521b\u5efa\u6570\u7ec4<code>arr = np.array([10, 20, 30, 20, 40])<\/code>\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7<code>np.where(arr == 20)<\/code>\u6765\u83b7\u53d6\u6240\u6709<code>20<\/code>\u7684\u4e0b\u6807\uff0c\u7ed3\u679c\u662f\u4e00\u4e2a\u5305\u542b\u4e0b\u6807\u7684\u6570\u7ec4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8fd4\u56de\u6570\u7ec4\uff08\u6216\u5217\u8868\uff09\u4e2d\u5143\u7d20\u7684\u4e0b\u6807\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4f7f\u7528index()\u65b9\u6cd5\u3001\u679a\u4e3e\uff08e [&hellip;]","protected":false},"author":3,"featured_media":988004,"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\/988000"}],"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=988000"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988000\/revisions"}],"predecessor-version":[{"id":988010,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988000\/revisions\/988010"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/988004"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=988000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=988000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=988000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}