{"id":1184653,"date":"2025-01-15T19:29:15","date_gmt":"2025-01-15T11:29:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1184653.html"},"modified":"2025-01-15T19:29:19","modified_gmt":"2025-01-15T11:29:19","slug":"python%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87%e5%80%bc%e7%a1%ae%e5%ae%9a%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1184653.html","title":{"rendered":"Python\u5982\u4f55\u901a\u8fc7\u503c\u786e\u5b9a\u7d22\u5f15"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25134028\/0c39db8b-c717-44f4-8e09-129326b75603.webp\" alt=\"Python\u5982\u4f55\u901a\u8fc7\u503c\u786e\u5b9a\u7d22\u5f15\" \/><\/p>\n<p><p> <strong>Python\u901a\u8fc7\u503c\u786e\u5b9a\u7d22\u5f15\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u7684index()\u65b9\u6cd5\u3001numpy\u5e93\u3001pandas\u5e93\u7b49\uff0c\u5177\u4f53\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f7f\u7528\u7684\u5bf9\u8c61\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5217\u8868\u7684index()\u65b9\u6cd5\u3001numpy\u7684where()\u51fd\u6570\u548cpandas\u7684\u7d22\u5f15\u65b9\u6cd5\u90fd\u53ef\u4ee5\u7528\u4e8e\u786e\u5b9a\u7d22\u5f15\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5217\u8868\u7684index()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u5217\u8868\u7684index()\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u503c\u7684\u7d22\u5f15\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u6709\u91cd\u590d\u7684\u503c\uff0cindex()\u65b9\u6cd5\u53ea\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u503c\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 20, 40]<\/p>\n<p>index = my_list.index(20)<\/p>\n<p>print(index)  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>numpy\u7684where()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u5904\u7406\uff0cnumpy\u5e93\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u9009\u62e9\u3002numpy\u7684where()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u6570\u7ec4\u4e2d\u6ee1\u8db3\u7279\u5b9a\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_array = np.array([10, 20, 30, 20, 40])<\/p>\n<p>indices = np.where(my_array == 20)<\/p>\n<p>print(indices)  # \u8f93\u51fa: (array([1, 3]),)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>pandas\u7684\u7d22\u5f15\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0cpandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002pandas\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u7d22\u5f15\u65b9\u6cd5\uff0c\u9002\u7528\u4e8eDataFrame\u548cSeries\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;A&#39;: [10, 20, 30, 20, 40]})<\/p>\n<p>indices = df.index[df[&#39;A&#39;] == 20].tolist()<\/p>\n<p>print(indices)  # \u8f93\u51fa: [1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u3001\u4f18\u7f3a\u70b9\u4ee5\u53ca\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u5217\u8868\u7684index()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u7684index()\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5217\u8868\u67e5\u627e\u64cd\u4f5c\uff0c\u7279\u522b\u662f\u5f53\u6211\u4eec\u53ea\u9700\u8981\u627e\u5230\u7b2c\u4e00\u4e2a\u5339\u914d\u503c\u7684\u7d22\u5f15\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>index()\u65b9\u6cd5\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list.index(value, start, end)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>value<\/code>\uff1a\u8981\u67e5\u627e\u7684\u503c\u3002<\/li>\n<li><code>start<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u67e5\u627e\u8303\u56f4\u7684\u8d77\u59cb\u7d22\u5f15\u3002<\/li>\n<li><code>end<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u67e5\u627e\u8303\u56f4\u7684\u7ed3\u675f\u7d22\u5f15\uff08\u4e0d\u5305\u62ec\uff09\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 20, 40]<\/p>\n<p>index = my_list.index(20)<\/p>\n<p>print(index)  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li>\u5982\u679c\u5217\u8868\u4e2d\u6ca1\u6709\u627e\u5230\u6307\u5b9a\u7684\u503c\uff0cindex()\u65b9\u6cd5\u4f1a\u629b\u51faValueError\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u8c03\u7528index()\u65b9\u6cd5\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528in\u64cd\u4f5c\u7b26\u68c0\u67e5\u503c\u662f\u5426\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\u3002<\/li>\n<li>\u4ec5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u503c\u7684\u7d22\u5f15\uff0c\u5982\u679c\u9700\u8981\u627e\u5230\u6240\u6709\u5339\u914d\u503c\u7684\u7d22\u5f15\uff0c\u9700\u8981\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5176\u4ed6\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 20, 40]<\/p>\n<p>value_to_find = 20<\/p>\n<p>if value_to_find in my_list:<\/p>\n<p>    index = my_list.index(value_to_find)<\/p>\n<p>    print(f&quot;\u7b2c\u4e00\u4e2a\u5339\u914d\u503c\u7684\u7d22\u5f15: {index}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(f&quot;\u503c {value_to_find} \u4e0d\u5b58\u5728\u4e8e\u5217\u8868\u4e2d&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u53ea\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u503c\u7684\u7d22\u5f15\uff0c\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u67e5\u627e\u6548\u7387\u8f83\u4f4e\u3002<\/li>\n<\/ul>\n<hr>\n<p><h2>\u4e8c\u3001numpy\u7684where()\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>numpy\u7684where()\u51fd\u6570\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u7ec4\u7684\u6570\u636e\u67e5\u627e\u64cd\u4f5c\uff0c\u7279\u522b\u662f\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>where()\u51fd\u6570\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numpy.where(condition, [x, y, ])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>condition<\/code>\uff1a\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u8fd4\u56de\u5e03\u5c14\u6570\u7ec4\u3002<\/li>\n<li><code>x, y<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u5f53condition\u4e3aTrue\u65f6\u8fd4\u56dex\u7684\u503c\uff0c\u4e3aFalse\u65f6\u8fd4\u56dey\u7684\u503c\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.array([10, 20, 30, 20, 40])<\/p>\n<p>indices = np.where(my_array == 20)<\/p>\n<p>print(indices)  # \u8f93\u51fa: (array([1, 3]),)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li>where()\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u5305\u542b\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u7d22\u5f15\u6570\u7ec4\u3002\u5982\u679c\u6570\u7ec4\u662f\u591a\u7ef4\u7684\uff0c\u8fd4\u56de\u7684\u5143\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u8868\u793a\u6bcf\u4e2a\u7ef4\u5ea6\u4e0a\u6ee1\u8db3\u6761\u4ef6\u7684\u7d22\u5f15\u3002<\/li>\n<li>\u53ef\u4ee5\u7ed3\u5408numpy\u7684\u5176\u4ed6\u51fd\u6570\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">my_array = np.array([[10, 20, 30], [20, 40, 50]])<\/p>\n<p>indices = np.where(my_array == 20)<\/p>\n<p>print(indices)  # \u8f93\u51fa: (array([0, 1]), array([1, 0]))<\/p>\n<h2><strong>\u83b7\u53d6\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20<\/strong><\/h2>\n<p>matched_elements = my_array[indices]<\/p>\n<p>print(matched_elements)  # \u8f93\u51fa: [20 20]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u652f\u6301\u591a\u7ef4\u6570\u7ec4\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u5bfc\u5165\u7b2c\u4e09\u65b9\u5e93numpy\uff0c\u5b66\u4e60\u6210\u672c\u8f83\u9ad8\u3002<\/li>\n<\/ul>\n<hr>\n<p><h2>\u4e09\u3001pandas\u7684\u7d22\u5f15\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>pandas\u7684\u7d22\u5f15\u65b9\u6cd5\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982DataFrame\u548cSeries\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u7d22\u5f15\u65b9\u6cd5\uff0c\u5e38\u7528\u7684\u6709\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15<\/li>\n<li>\u4f7f\u7528loc\u548ciloc\u65b9\u6cd5<\/li>\n<li>\u4f7f\u7528index\u5c5e\u6027<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;A&#39;: [10, 20, 30, 20, 40]})<\/p>\n<p>indices = df.index[df[&#39;A&#39;] == 20].tolist()<\/p>\n<p>print(indices)  # \u8f93\u51fa: [1, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li>pandas\u7684\u7d22\u5f15\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u7d22\u5f15\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528tolist()\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/li>\n<li>pandas\u652f\u6301\u66f4\u590d\u6742\u7684\u6761\u4ef6\u7d22\u5f15\u548c\u591a\u5217\u7d22\u5f15\uff0c\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u4e2d\u7684\u5404\u79cd\u9700\u6c42\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">df = pd.DataFrame({&#39;A&#39;: [10, 20, 30, 20, 40], &#39;B&#39;: [5, 15, 25, 35, 45]})<\/p>\n<p>indices = df.index[(df[&#39;A&#39;] == 20) &amp; (df[&#39;B&#39;] &gt; 30)].tolist()<\/p>\n<p>print(indices)  # \u8f93\u51fa: [3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u652f\u6301\u590d\u6742\u6761\u4ef6\u7d22\u5f15\u3002<\/li>\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u5bfc\u5165\u7b2c\u4e09\u65b9\u5e93pandas\uff0c\u5b66\u4e60\u6210\u672c\u8f83\u9ad8\u3002<\/li>\n<\/ul>\n<hr>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python\u4e2d\u901a\u8fc7\u503c\u786e\u5b9a\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\u548c\u6570\u636e\u7c7b\u578b\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5217\u8868\u67e5\u627e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u7684index()\u65b9\u6cd5\uff1b\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u7ec4\u7684\u6570\u636e\u67e5\u627e\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u7684where()\u51fd\u6570\uff1b\u5bf9\u4e8e\u7ed3\u6784\u5316\u6570\u636e\u7684\u5206\u6790\u548c\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u7684\u7d22\u5f15\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u65b9\u6cd5\u5bf9\u6bd4<\/h3>\n<\/p>\n<ul>\n<li><strong>\u5217\u8868\u7684index()\u65b9\u6cd5<\/strong>\uff1a\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\uff0c\u53ea\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u503c\u7684\u7d22\u5f15\u3002<\/li>\n<li><strong>numpy\u7684where()\u51fd\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u652f\u6301\u591a\u7ef4\u6570\u7ec4\uff0c\u529f\u80fd\u5f3a\u5927\u3002<\/li>\n<li><strong>pandas\u7684\u7d22\u5f15\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u652f\u6301\u590d\u6742\u6761\u4ef6\u7d22\u5f15\u548c\u591a\u5217\u7d22\u5f15\uff0c\u529f\u80fd\u4e30\u5bcc\u3002<\/li>\n<\/ul>\n<p><h3>\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u6570\u636e\u7c7b\u578b\u548c\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5728\u6570\u636e\u5206\u6790\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528pandas\u53ef\u4ee5\u63d0\u4f9b\u66f4\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\uff1b\u5728\u79d1\u5b66\u8ba1\u7b97\u9879\u76ee\u4e2d\uff0cnumpy\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u800c\u5728\u7b80\u5355\u7684\u5217\u8868\u67e5\u627e\u64cd\u4f5c\u4e2d\uff0c\u5217\u8868\u7684index()\u65b9\u6cd5\u8db3\u591f\u6ee1\u8db3\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\uff0c\u63d0\u5347\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u901a\u8fc7\u503c\u627e\u5230\u5217\u8868\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>list.index(value)<\/code>\u65b9\u6cd5\u6765\u67e5\u627e\u67d0\u4e2a\u503c\u5728\u5217\u8868\u4e2d\u7684\u7d22\u5f15\u3002\u5982\u679c\u8be5\u503c\u5b58\u5728\u4e8e\u5217\u8868\u4e2d\uff0c<code>index()<\/code>\u65b9\u6cd5\u5c06\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u7d22\u5f15\uff1b\u5982\u679c\u8be5\u503c\u4e0d\u5728\u5217\u8868\u4e2d\uff0c\u7a0b\u5e8f\u5c06\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u5148\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u68c0\u67e5\u503c\u662f\u5426\u5b58\u5728\u3002<\/p>\n<p><strong>\u5982\u679c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u503c\uff0c\u6211\u8be5\u5982\u4f55\u627e\u5230\u6240\u6709\u5339\u914d\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u627e\u5230\u6240\u6709\u5339\u914d\u503c\u7684\u7d22\u5f15\u3002\u901a\u8fc7\u904d\u5386\u5217\u8868\u5e76\u6bd4\u8f83\u6bcf\u4e2a\u5143\u7d20\u4e0e\u76ee\u6807\u503c\uff0c\u5982\u679c\u5339\u914d\uff0c\u5c31\u5c06\u7d22\u5f15\u6dfb\u52a0\u5230\u65b0\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff1a<code>[index for index, value in enumerate(your_list) if value == target_value]<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u8fd4\u56de\u6240\u6709\u5339\u914d\u503c\u7684\u7d22\u5f15\u5217\u8868\u3002<\/p>\n<p><strong>\u5728\u5b57\u5178\u4e2d\uff0c\u5982\u4f55\u901a\u8fc7\u503c\u627e\u5230\u952e\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u5728\u5b57\u5178\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u5178\u7684<code>items()<\/code>\u6765\u627e\u5230\u503c\u5bf9\u5e94\u7684\u952e\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u9ad8\u6548\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4f8b\u5982\uff1a<code>[key for key, value in your_dict.items() if value == target_value]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u6240\u6709\u4e0e\u7ed9\u5b9a\u503c\u5339\u914d\u7684\u952e\u7684\u5217\u8868\u3002\u8bf7\u6ce8\u610f\uff0c\u5b57\u5178\u4e2d\u7684\u503c\u53ef\u4ee5\u91cd\u590d\uff0c\u56e0\u6b64\u53ef\u80fd\u4f1a\u8fd4\u56de\u591a\u4e2a\u952e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u901a\u8fc7\u503c\u786e\u5b9a\u7d22\u5f15\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u7684index()\u65b9\u6cd5\u3001numpy\u5e93\u3001pandas\u5e93\u7b49\uff0c\u5177\u4f53 [&hellip;]","protected":false},"author":3,"featured_media":1184660,"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\/1184653"}],"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=1184653"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184653\/revisions"}],"predecessor-version":[{"id":1184663,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1184653\/revisions\/1184663"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1184660"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1184653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1184653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1184653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}