{"id":1097277,"date":"2025-01-08T15:10:11","date_gmt":"2025-01-08T07:10:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097277.html"},"modified":"2025-01-08T15:10:14","modified_gmt":"2025-01-08T07:10:14","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e5%88%97%e8%a1%a8%e4%b8%ad%e5%85%83%e7%b4%a0%e7%9a%84%e4%bd%8d%e7%bd%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097277.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212125\/1be8c3ba-3be2-47b6-9178-eb49cddeaedd.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u63d0\u53d6\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u63d0\u53d6\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u5217\u8868\u63a8\u5bfc\u5f0f\u3001enumerate\u51fd\u6570\u3001index\u65b9\u6cd5\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u591f\u9ad8\u6548\u3001\u7075\u6d3b\u5730\u67e5\u627e\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u5e76\u4e14\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u6216\u590d\u6742\u7ed3\u6784\u65f6\u5c24\u4e3a\u6709\u7528\u3002<strong>\u4f7f\u7528index\u65b9\u6cd5\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528enumerate\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528index\u65b9\u6cd5<\/strong>\u662f\u6700\u5e38\u89c1\u4e14\u7b80\u6d01\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5bfb\u627e\u5355\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528index\u65b9\u6cd5<\/strong>\uff1aPython\u7684\u5217\u8868\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684index\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u67e5\u627e\u67d0\u4e2a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u7684\u4f4d\u7f6e\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\u5219\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>position = my_list.index(3)<\/p>\n<p>print(position)  # \u8f93\u51fa: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\uff0cindex\u65b9\u6cd5\u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528index\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>index\u65b9\u6cd5\u662fPython\u5217\u8868\u5bf9\u8c61\u7684\u4e00\u4e2a\u5185\u7f6e\u65b9\u6cd5\uff0c\u7528\u4e8e\u67e5\u627e\u67d0\u4e2a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u7684\u4f4d\u7f6e\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\u5219\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>position = my_list.index(3)<\/p>\n<p>print(position)  # \u8f93\u51fa: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\uff0cindex\u65b9\u6cd5\u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 3, 4, 5]<\/p>\n<p>position = my_list.index(3)<\/p>\n<p>print(position)  # \u8f93\u51fa: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u627e\u5230\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5176\u4ed6\u65b9\u6cd5\u6765\u5b9e\u73b0\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\uff08list comprehension\uff09\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u5217\u8868\u751f\u6210\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u8fc7\u6ee4\u548c\u8f6c\u6362\u5217\u8868\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u627e\u6240\u6709\u5339\u914d\u5143\u7d20\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 3, 4, 5]<\/p>\n<p>positions = [index for index, value in enumerate(my_list) if value == 3]<\/p>\n<p>print(positions)  # \u8f93\u51fa: [2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cenumerate\u51fd\u6570\u4f1a\u751f\u6210\u4e00\u4e2a\u5305\u542b\u7d22\u5f15\u548c\u503c\u7684\u5143\u7ec4\u5e8f\u5217\uff0c\u7136\u540e\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u7b5b\u9009\u51fa\u503c\u7b49\u4e8e\u76ee\u6807\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528enumerate\u51fd\u6570<\/h3>\n<\/p>\n<p><p>enumerate\u51fd\u6570\u662fPython\u5185\u7f6e\u51fd\u6570\u4e4b\u4e00\uff0c\u5b83\u4f1a\u751f\u6210\u4e00\u4e2a\u5305\u542b\u7d22\u5f15\u548c\u503c\u7684\u5143\u7ec4\u5e8f\u5217\u3002\u7ed3\u5408for\u5faa\u73af\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u627e\u7279\u5b9a\u5143\u7d20\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>for index, value in enumerate(my_list):<\/p>\n<p>    if value == 3:<\/p>\n<p>        print(index)  # \u8f93\u51fa: 2<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u5728\u627e\u5230\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\u65f6\u7acb\u5373\u505c\u6b62\u641c\u7d22\uff0c\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u904d\u5386\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0cnumpy\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002numpy\u662f\u4e00\u4e2a\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528numpy\u7684where\u51fd\u6570\u67e5\u627e\u7279\u5b9a\u5143\u7d20\u7684\u4f4d\u7f6e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_list = [1, 2, 3, 3, 4, 5]<\/p>\n<p>my_array = np.array(my_list)<\/p>\n<p>positions = np.where(my_array == 3)<\/p>\n<p>print(positions)  # \u8f93\u51fa: (array([2, 3]),)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>numpy\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u7ed3\u6784\u5316\u6570\u636e\u7684\u64cd\u4f5c\u3002\u5982\u679c\u5217\u8868\u6570\u636e\u9700\u8981\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u8f6c\u6362\u6210DataFrame\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>my_list = [1, 2, 3, 3, 4, 5]<\/p>\n<p>df = pd.DataFrame({&#39;values&#39;: my_list})<\/p>\n<p>positions = df.index[df[&#39;values&#39;] == 3].tolist()<\/p>\n<p>print(positions)  # \u8f93\u51fa: [2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u590d\u6742\u6570\u636e\u5904\u7406\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u67e5\u627e\u5143\u7d20\u4f4d\u7f6e\u65f6\uff0c\u9700\u8981\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u5f53\u5143\u7d20\u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u65f6\uff0cindex\u65b9\u6cd5\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>try:<\/p>\n<p>    position = my_list.index(6)<\/p>\n<p>except ValueError:<\/p>\n<p>    position = -1<\/p>\n<p>print(position)  # \u8f93\u51fa: -1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u5f02\u5e38\u800c\u4e2d\u65ad\uff0c\u5e76\u63d0\u4f9b\u66f4\u53cb\u597d\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u63d0\u53d6\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e\u5728Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ecindex\u65b9\u6cd5\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001enumerate\u51fd\u6570\u3001numpy\u5e93\u548cpandas\u5e93\u7b49\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<strong>index\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u573a\u666f<\/strong>\uff0c<strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u548cenumerate\u51fd\u6570\u9002\u7528\u4e8e\u590d\u6742\u7b5b\u9009<\/strong>\uff0c<strong>numpy\u548cpandas\u5219\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u7efc\u5408\u8003\u8651\u6027\u80fd\u3001\u53ef\u8bfb\u6027\u548c\u5f02\u5e38\u5904\u7406\u7b49\u56e0\u7d20\uff0c\u9009\u62e9\u6700\u4f73\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u627e\u7279\u5b9a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list.index()<\/code>\u65b9\u6cd5\u6765\u67e5\u627e\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\u4f4d\u7f6e\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\u7d22\u5f15\u662f2\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u5143\u7d20\uff0c\u5982\u4f55\u63d0\u53d6\u6240\u6709\u5143\u7d20\u7684\u4f4d\u7f6e\uff1f<\/strong><br \/>\u5728\u5217\u8868\u4e2d\uff0c\u5982\u679c\u5b58\u5728\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>enumerate()<\/code>\u51fd\u6570\u6765\u63d0\u53d6\u6240\u6709\u76f8\u540c\u5143\u7d20\u7684\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u5217\u8868<code>my_list = [10, 20, 30, 20, 40]<\/code>\uff0c\u60f3\u8981\u627e\u5230\u6240\u6709<code>20<\/code>\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528<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\u7d22\u5f151\u548c3\u7684\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5143\u7d20\u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u67e5\u627e\u5143\u7d20\u7684\u7d22\u5f15\u65f6\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff0c<code>list.index()<\/code>\u65b9\u6cd5\u4f1a\u629b\u51fa<code>ValueError<\/code>\u3002\u4e3a\u4e86\u4f18\u96c5\u5730\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5148\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5728\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>if 30 in my_list:<\/code>\u6765\u68c0\u67e5<code>30<\/code>\u662f\u5426\u5728<code>my_list<\/code>\u4e2d\uff0c\u5982\u679c\u5b58\u5728\uff0c\u518d\u4f7f\u7528<code>my_list.index(30)<\/code>\u6765\u83b7\u53d6\u7d22\u5f15\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u56e0\u9519\u8bef\u800c\u4e2d\u65ad\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u63d0\u53d6\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e \u5728Python\u4e2d\u63d0\u53d6\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u5217\u8868\u63a8\u5bfc\u5f0f\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1097285,"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\/1097277"}],"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=1097277"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097277\/revisions"}],"predecessor-version":[{"id":1097286,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097277\/revisions\/1097286"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097285"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}