{"id":1018274,"date":"2024-12-27T12:40:21","date_gmt":"2024-12-27T04:40:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1018274.html"},"modified":"2024-12-27T12:40:23","modified_gmt":"2024-12-27T04:40:23","slug":"python%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e6%95%b0%e7%bb%84%e4%bd%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1018274.html","title":{"rendered":"python\u5982\u4f55\u8fd4\u56de\u6570\u7ec4\u4f4d\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161017\/c21a70a7-6a39-4b15-b1a8-b301cf77eb4c.webp\" alt=\"python\u5982\u4f55\u8fd4\u56de\u6570\u7ec4\u4f4d\u7f6e\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u53ef\u4ee5\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u3001\u5faa\u73af\u904d\u5386\u3001\u4ee5\u53caNumPy\u5e93\u7684<code>numpy.where()<\/code>\u51fd\u6570\u6765\u8fd4\u56de\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u72ec\u7279\u7684\u4f7f\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<strong><code>index()<\/code>\u65b9\u6cd5\u9002\u7528\u4e8e\u5217\u8868\u5e76\u8fd4\u56de\u7b2c\u4e00\u6b21\u51fa\u73b0\u5143\u7d20\u7684\u7d22\u5f15<\/strong>\uff0c\u800c\u5faa\u73af\u904d\u5386\u5219\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u67e5\u627e\u64cd\u4f5c\uff0cNumPy\u5e93\u5219\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u7ec4\u5e76\u80fd\u9ad8\u6548\u627e\u5230\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u7d22\u5f15\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684<code>index()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong><code>index()<\/code>\u65b9\u6cd5\u662f\u7528\u4e8e\u5217\u8868\u7684\u5185\u7f6e\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u6307\u5b9a\u503c\u5728\u5217\u8868\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u7684\u7d22\u5f15<\/strong>\u3002\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8c03\u7528\u8be5\u65b9\u6cd5\u5e76\u4f20\u5165\u9700\u8981\u67e5\u627e\u7684\u503c\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u7b80\u5355\u7684\u6574\u6570\u5217\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_list.index(5)<\/code>\u6765\u67e5\u627e\u6570\u5b575\u5728\u5217\u8868\u4e2d\u7684\u4f4d\u7f6e\u3002\u5982\u679c\u8be5\u503c\u4e0d\u5728\u5217\u8868\u4e2d\uff0c\u4f1a\u629b\u51fa<code>ValueError<\/code>\u5f02\u5e38\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u9700\u8981\u505a\u597d\u5f02\u5e38\u5904\u7406\u3002<code>index()<\/code>\u65b9\u6cd5\u7684\u6548\u7387\u8f83\u9ad8\uff0c\u9002\u7528\u4e8e\u4e2d\u5c0f\u578b\u5217\u8868\u7684\u67e5\u627e\u64cd\u4f5c\uff0c\u4f46\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u5c24\u5176\u662f\u9700\u8981\u67e5\u627e\u591a\u4e2a\u5339\u914d\u9879\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001<code>INDEX()<\/code>\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>index()<\/code>\u65b9\u6cd5\u662fPython\u4e2d\u7528\u4e8e\u67e5\u627e\u5217\u8868\u4e2d\u5143\u7d20\u4f4d\u7f6e\u7684\u4e00\u4e2a\u7b80\u5355\u4e14\u5e38\u7528\u7684\u5de5\u5177\u3002\u5b83\u4e0d\u4ec5\u5bb9\u6613\u4f7f\u7528\uff0c\u800c\u4e14\u5728\u5904\u7406\u5c0f\u578b\u5217\u8868\u65f6\u8868\u73b0\u51fa\u8272\u3002\u5176\u4e3b\u8981\u529f\u80fd\u662f\u8fd4\u56de\u5217\u8868\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u6307\u5b9a\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>index()<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ea\u9700\u8981\u5c06\u9700\u8981\u67e5\u627e\u7684\u5143\u7d20\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>index_position = my_list.index(30)<\/p>\n<p>print(index_position)  # \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\u8fd4\u56de\u5143\u7d20<code>30<\/code>\u5728<code>my_list<\/code>\u4e2d\u7684\u7d22\u5f15\uff0c\u5373<code>2<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u67e5\u627e\u7684\u5143\u7d20\u4e0d\u5728\u5217\u8868\u4e2d\uff0c<code>index()<\/code>\u65b9\u6cd5\u4f1a\u629b\u51fa\u4e00\u4e2a<code>ValueError<\/code>\u5f02\u5e38\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>try-except<\/code>\u5757\u6765\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    index_position = my_list.index(60)<\/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>\u8fd9\u79cd\u505a\u6cd5\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u7a0b\u5e8f\u56e0\u5f02\u5e38\u800c\u4e2d\u65ad\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u8003\u8651<\/strong><\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1<code>index()<\/code>\u65b9\u6cd5\u5728\u5c0f\u578b\u5217\u8868\u4e2d\u6548\u7387\u5f88\u9ad8\uff0c\u4f46\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u5217\u8868\u65f6\uff0c\u7531\u4e8e\u5176\u7ebf\u6027\u641c\u7d22\u7684\u7279\u6027\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u5f71\u54cd\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u5176\u4ed6\u66f4\u9ad8\u6548\u7684\u7b97\u6cd5\u6216\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<hr>\n<p><p>\u4e8c\u3001\u5faa\u73af\u904d\u5386<\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5728\u5217\u8868\u6216\u6570\u7ec4\u4e2d\u67e5\u627e\u591a\u4e2a\u5339\u914d\u9879\uff0c\u6216\u9700\u8981\u5728\u590d\u6742\u6761\u4ef6\u4e0b\u67e5\u627e\u65f6\uff0c\u5faa\u73af\u904d\u5386\u662f\u4e00\u79cd\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u904d\u5386\u6bcf\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u7cbe\u7ec6\u63a7\u5236\u67e5\u627e\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9e\u73b0\u57fa\u672c\u67e5\u627e<\/strong><\/p>\n<\/p>\n<p><p>\u5faa\u73af\u904d\u5386\u5141\u8bb8\u6211\u4eec\u624b\u52a8\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u7b26\u5408\u6761\u4ef6\uff0c\u5e76\u8bb0\u5f55\u5176\u4f4d\u7f6e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50, 30]<\/p>\n<p>target = 30<\/p>\n<p>positions = []<\/p>\n<p>for index, value in enumerate(my_list):<\/p>\n<p>    if value == target:<\/p>\n<p>        positions.append(index)<\/p>\n<p>print(positions)  # \u8f93\u51fa [2, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u627e\u5230\u5217\u8868\u4e2d\u6240\u6709\u7b49\u4e8e<code>30<\/code>\u7684\u5143\u7d20\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7075\u6d3b\u6027\u4e0e\u590d\u6742\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5faa\u73af\u904d\u5386\u7684\u4e00\u4e2a\u663e\u8457\u4f18\u52bf\u662f\u7075\u6d3b\u6027\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u4e49\u67e5\u627e\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u6240\u6709\u5076\u6570\u6216\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">positions = [index for index, value in enumerate(my_list) if value % 2 == 0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5c24\u5176\u9002\u5408\u590d\u6742\u7684\u67e5\u627e\u903b\u8f91\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u8003\u8651<\/strong><\/p>\n<\/p>\n<p><p>\u867d\u7136\u5faa\u73af\u904d\u5386\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\uff0c\u4f46\u5728\u975e\u5e38\u5927\u7684\u5217\u8868\u4e2d\uff0c\u6027\u80fd\u4ecd\u7136\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u7684\u95ee\u9898\u3002\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u4f7f\u7528NumPy\u7b49\u5e93\u53ef\u80fd\u4f1a\u66f4\u6709\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<hr>\n<p><p>\u4e09\u3001NUMPY\u5e93\u7684<code>WHERE()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>NumPy\u5e93\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u4e13\u4e3a\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u800c\u8bbe\u8ba1\u3002\u5176<code>numpy.where()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u9ad8\u6548\u67e5\u627e\u6570\u7ec4\u4e2d\u5143\u7d20\u4f4d\u7f6e\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>numpy.where()<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u7d22\u5f15\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>my_array = np.array([10, 20, 30, 40, 50, 30])<\/p>\n<p>indices = np.where(my_array == 30)<\/p>\n<p>print(indices)  # \u8f93\u51fa (array([2, 5]),)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u7d22\u5f15\u7684\u5143\u7ec4\uff0c\u9002\u7528\u4e8e\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u7684<code>where()<\/code>\u51fd\u6570\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u548c\u591a\u7ef4\u6570\u7ec4\u3002\u5176\u77e2\u91cf\u5316\u64cd\u4f5c\u4f7f\u5f97\u67e5\u627e\u8fc7\u7a0b\u975e\u5e38\u9ad8\u6548\uff0c\u9002\u5408\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u66f4\u591a\u529f\u80fd<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u7b80\u5355\u7684\u67e5\u627e\u5916\uff0c<code>numpy.where()<\/code>\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6761\u4ef6\u9009\u62e9\u548c\u66ff\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = np.where(my_array &gt; 25, my_array, -1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u4e2d\u6240\u6709\u5927\u4e8e<code>25<\/code>\u7684\u5143\u7d20\u4fdd\u6301\u4e0d\u53d8\uff0c\u5176\u4f59\u5143\u7d20\u88ab\u66ff\u6362\u4e3a<code>-1<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<hr>\n<p><p>\u56db\u3001\u5176\u4ed6\u67e5\u627e\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u7528\u7684\u67e5\u627e\u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u6280\u5de7\u548c\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728Python\u4e2d\u67e5\u627e\u6570\u7ec4\u5143\u7d20\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u5178\u6620\u5c04<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u67e5\u627e\u901f\u5ea6\u8981\u6c42\u5f88\u9ad8\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u6765\u63d0\u9ad8\u6548\u7387\u3002\u5c06\u5143\u7d20\u503c\u6620\u5c04\u5230\u7d22\u5f15\u4f4d\u7f6e\uff0c\u67e5\u627e\u65f6\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u5b57\u5178\u8bbf\u95ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>index_map = {value: index for index, value in enumerate(my_list)}<\/p>\n<p>print(index_map[30])  # \u8f93\u51fa 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u67e5\u627e\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u96c6\u5408\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>\u96c6\u5408\u53ef\u4ee5\u7528\u4e8e\u5feb\u901f\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\uff0c\u4f46\u7531\u4e8e\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u65e0\u6cd5\u76f4\u63a5\u8fd4\u56de\u7d22\u5f15\u3002\u4e0d\u8fc7\u53ef\u4ee5\u7ed3\u5408\u5217\u8868\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_set = set(my_list)<\/p>\n<p>if 30 in my_set:<\/p>\n<p>    position = my_list.index(30)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u63d0\u9ad8\u67e5\u627e\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5927\u6570\u636e\u96c6\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5b9a\u5236\u5316\u7684\u67e5\u627e\u903b\u8f91\uff0c\u8fd9\u65f6\u53ef\u4ee5\u7f16\u5199\u81ea\u5df1\u7684\u51fd\u6570\u6765\u5904\u7406\u3002\u4f8b\u5982\uff0c\u67e5\u627e\u6240\u6709\u6ee1\u8db3\u67d0\u4e2a\u590d\u6742\u6761\u4ef6\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_positions(lst, condition_func):<\/p>\n<p>    return [index for index, value in enumerate(lst) if condition_func(value)]<\/p>\n<p>positions = find_positions(my_list, lambda x: x &gt; 25)<\/p>\n<p>print(positions)  # \u8f93\u51fa [2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\uff0c\u9002\u5408\u4e8e\u5404\u79cd\u590d\u6742\u7684\u67e5\u627e\u9700\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<hr>\n<p><p>\u4e94\u3001\u6027\u80fd\u4f18\u5316\u7b56\u7565<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u67e5\u627e\u64cd\u4f5c\u7684\u6027\u80fd\u53ef\u80fd\u6210\u4e3a\u74f6\u9888\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u7b56\u7565\uff0c\u5e2e\u52a9\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<\/p>\n<p><p>\u6839\u636e\u67e5\u627e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6548\u7387\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u5feb\u901f\u67e5\u627e\uff0c\u4f7f\u7528NumPy\u6570\u7ec4\u8fdb\u884c\u5927\u89c4\u6a21\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u67e5\u627e<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u53ef\u80fd\uff0c\u5c3d\u91cf\u51cf\u5c11\u67e5\u627e\u64cd\u4f5c\u7684\u6b21\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u7f13\u5b58\u7ed3\u679c\u3001\u63d0\u524d\u8fc7\u6ee4\u6570\u636e\u7b49\u65b9\u5f0f\u4f18\u5316\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6279\u91cf\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u67e5\u627e\u591a\u4e2a\u5143\u7d20\u7684\u573a\u666f\uff0c\u8003\u8651\u6279\u91cf\u5904\u7406\u3002\u4f8b\u5982\u4f7f\u7528NumPy\u7684\u77e2\u91cf\u5316\u64cd\u4f5c\u4e00\u6b21\u6027\u5904\u7406\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e76\u884c\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\uff0c\u4ee5\u63d0\u9ad8\u67e5\u627e\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b97\u6cd5\u4f18\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u53ef\u80fd\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u67e5\u627e\u7b97\u6cd5\u3002\u4f8b\u5982\uff0c\u4e8c\u5206\u67e5\u627e\u9002\u7528\u4e8e\u5df2\u6392\u5e8f\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u67e5\u627e\u901f\u5ea6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u4f18\u5316\u67e5\u627e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u9ad8\u6548\u5730\u627e\u5230\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u7b56\u7565\uff0c\u5c06\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\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\u627e\u5230\u7279\u5b9a\u5143\u7d20\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list.index()<\/code>\u65b9\u6cd5\u6765\u627e\u5230\u7279\u5b9a\u5143\u7d20\u7684\u7d22\u5f15\u3002\u5982\u679c\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u5b58\u5728\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u8fd4\u56de\u5b83\u7684\u7b2c\u4e00\u4e2a\u51fa\u73b0\u4f4d\u7f6e\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [10, 20, 30, 40]\nindex = my_list.index(30)  # \u8fd4\u56de 2\n<\/code><\/pre>\n<p>\u5982\u679c\u8be5\u5143\u7d20\u4e0d\u5b58\u5728\uff0cPython\u4f1a\u629b\u51fa\u4e00\u4e2a<code>ValueError<\/code>\u5f02\u5e38\uff0c\u5efa\u8bae\u4f7f\u7528<code>in<\/code>\u5173\u952e\u5b57\u5148\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u3002<\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6\u6570\u7ec4\u4e2d\u6240\u6709\u7b26\u5408\u6761\u4ef6\u5143\u7d20\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u82e5\u9700\u8981\u627e\u5230\u6570\u7ec4\u4e2d\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>enumerate()<\/code>\u51fd\u6570\u3002\u8fd9\u6837\u53ef\u4ee5\u904d\u5386\u6570\u7ec4\u5e76\u83b7\u53d6\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u6240\u6709\u5927\u4e8e25\u7684\u5143\u7d20\u7684\u7d22\u5f15\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [10, 20, 30, 40]\nindices = [i for i, x in enumerate(my_list) if x &gt; 25]  # \u8fd4\u56de [2, 3]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u8fdb\u884c\u7b5b\u9009\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6570\u7ec4\u4e2d\u91cd\u590d\u5143\u7d20\u7684\u7d22\u5f15\u8fd4\u56de\uff1f<\/strong><br \/>\u5728\u5b58\u5728\u91cd\u590d\u5143\u7d20\u7684\u60c5\u51b5\u4e0b\uff0c\u82e5\u60f3\u83b7\u53d6\u6240\u6709\u91cd\u590d\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u540c\u6837\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u6bd4\u5982\uff0c\u83b7\u53d6\u6240\u6709\u503c\u4e3a20\u7684\u5143\u7d20\u7684\u7d22\u5f15\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [10, 20, 20, 30]\nindices = [i for i, x in enumerate(my_list) if x == 20]  # \u8fd4\u56de [1, 2]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u4e86\u6bcf\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u90fd\u80fd\u88ab\u8bb0\u5f55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528index()\u65b9\u6cd5\u3001\u5faa\u73af\u904d\u5386\u3001\u4ee5\u53caNumPy\u5e93\u7684numpy.where()\u51fd\u6570\u6765\u8fd4\u56de [&hellip;]","protected":false},"author":3,"featured_media":1018278,"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\/1018274"}],"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=1018274"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018274\/revisions"}],"predecessor-version":[{"id":1018282,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018274\/revisions\/1018282"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1018278"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1018274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1018274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1018274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}