{"id":1176251,"date":"2025-01-15T17:42:34","date_gmt":"2025-01-15T09:42:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1176251.html"},"modified":"2025-01-15T17:42:37","modified_gmt":"2025-01-15T09:42:37","slug":"python%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8%e7%b4%a2%e5%bc%95%e6%89%be%e5%88%b0%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1176251.html","title":{"rendered":"python\u5982\u4f55\u5229\u7528\u7d22\u5f15\u627e\u5230\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25111455\/18b93686-77df-4954-806c-9f07945a9256.webp\" alt=\"python\u5982\u4f55\u5229\u7528\u7d22\u5f15\u627e\u5230\u6570\u5b57\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5229\u7528\u7d22\u5f15\u627e\u5230\u6570\u5b57\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec<strong>\u4f7f\u7528\u5217\u8868\u7684\u7d22\u5f15\u65b9\u6cd5\u3001\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u65b9\u6cd5\u3001\u4ee5\u53ca\u901a\u8fc7NumPy\u5e93\u8fdb\u884c\u9ad8\u7ea7\u7d22\u5f15<\/strong>\u3002\u5176\u4e2d\uff0c\u5217\u8868\u7684\u7d22\u5f15\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5217\u8868\u662fPython\u4e2d\u6700\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\u3002\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u7684<code>index<\/code>\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u627e\u5230\u5217\u8868\u4e2d\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<strong>\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>numbers = [10, 20, 30, 40, 50]<\/code>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>numbers.index(30)<\/code>\u6765\u627e\u5230\u6570\u5b5730\u7684\u7d22\u5f15\u4f4d\u7f6e<\/strong>\uff0c\u7ed3\u679c\u4e3a2\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5229\u7528\u5217\u8868\u7684\u7d22\u5f15\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8\u591a\u4e2a\u5143\u7d20\u3002\u627e\u5230\u5217\u8868\u4e2d\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u5217\u8868\u7684<code>index<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>index_of_30 = numbers.index(30)<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5217\u8868<code>numbers<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>numbers.index(30)<\/code>\u6765\u627e\u5230\u6570\u5b5730\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u7ed3\u679c\u4e3a2\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u5217\u8868\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u6570\u5b57\uff0c<code>index<\/code>\u65b9\u6cd5\u4f1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5229\u7528\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6570\u5b57\u53ef\u80fd\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u4e2d\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>find<\/code>\u6216<code>index<\/code>\u65b9\u6cd5\u6765\u627e\u5230\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<code>find<\/code>\u65b9\u6cd5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u7d22\u5f15\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u5219\u8fd4\u56de-1\uff1b<code>index<\/code>\u65b9\u6cd5\u4e0e<code>find<\/code>\u7c7b\u4f3c\uff0c\u4f46\u5982\u679c\u6ca1\u6709\u627e\u5230\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;The numbers are 10, 20, 30, 40, and 50.&quot;<\/p>\n<p>index_of_30 = text.find(&quot;30&quot;)<\/p>\n<p>print(f&quot;Index of &#39;30&#39;: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5b57\u7b26\u4e32<code>text<\/code>\uff0c\u4f7f\u7528<code>text.find(&quot;30&quot;)<\/code>\u6765\u627e\u5230\u5b57\u7b26\u4e32&quot;30&quot;\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u7ed3\u679c\u4e3a16\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5229\u7528NumPy\u5e93\u8fdb\u884c\u9ad8\u7ea7\u7d22\u5f15<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u7684\u4e00\u4e2a\u5f3a\u5927\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7d22\u5f15\u548c\u641c\u7d22\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u591a\u7ef4\u6570\u7ec4\u3002\u5229\u7528NumPy\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u627e\u5230\u6570\u7ec4\u4e2d\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>numbers = np.array([10, 20, 30, 40, 50])<\/p>\n<p>index_of_30 = np.where(numbers == 30)<\/p>\n<p>print(f&quot;Index of 30: {index_of_30[0][0]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528NumPy\u521b\u5efa\u4e86\u4e00\u4e2a\u6570\u7ec4<code>numbers<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>np.where(numbers == 30)<\/code>\u6765\u627e\u5230\u6570\u5b5730\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u7ed3\u679c\u4e3a2\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5229\u7528\u5faa\u73af\u67e5\u627e\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u65b9\u6cd5\u548c\u5e93\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868\u6216\u6570\u7ec4\u6765\u67e5\u627e\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982\u524d\u9762\u7684\u65b9\u6cd5\u7b80\u6d01\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>target = 30<\/p>\n<p>index_of_30 = -1<\/p>\n<p>for i, num in enumerate(numbers):<\/p>\n<p>    if num == target:<\/p>\n<p>        index_of_30 = i<\/p>\n<p>        break<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868<code>numbers<\/code>\uff0c\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\uff0c\u5982\u679c\u627e\u5230\u76ee\u6807\u6570\u5b57\uff0c\u5219\u8bb0\u5f55\u5176\u7d22\u5f15\u5e76\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5229\u7528\u5b57\u5178\u67e5\u627e\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u6570\u5b57\u53ca\u5176\u7d22\u5f15\u3002\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7\u952e\u5feb\u901f\u8bbf\u95ee\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>index_dict = {num: i for i, num in enumerate(numbers)}<\/p>\n<p>index_of_30 = index_dict.get(30, -1)<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u6570\u5b57\u5230\u7d22\u5f15\u7684\u6620\u5c04<code>index_dict<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>index_dict.get(30, -1)<\/code>\u6765\u67e5\u627e\u6570\u5b5730\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u5219\u8fd4\u56de-1\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5229\u7528Pandas\u5e93\u67e5\u627e\u7d22\u5f15<\/p>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5f3a\u5927\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002\u5229\u7528Pandas\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u548c\u67e5\u627e\u5927\u578b\u6570\u636e\u96c6\u4e2d\u7684\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>numbers = pd.Series([10, 20, 30, 40, 50])<\/p>\n<p>index_of_30 = numbers[numbers == 30].index[0]<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u521b\u5efa\u4e86\u4e00\u4e2a<code>Series<\/code>\u5bf9\u8c61<code>numbers<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15<code>numbers[numbers == 30]<\/code>\u6765\u627e\u5230\u6570\u5b5730\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5229\u7528\u9012\u5f52\u67e5\u627e\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u53ef\u4ee5\u7528\u4e8e\u89e3\u51b3\u8bb8\u591a\u590d\u6742\u95ee\u9898\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u67e5\u627e\u5217\u8868\u4e2d\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_index(numbers, target, start=0):<\/p>\n<p>    if start &gt;= len(numbers):<\/p>\n<p>        return -1<\/p>\n<p>    if numbers[start] == target:<\/p>\n<p>        return start<\/p>\n<p>    return find_index(numbers, target, start + 1)<\/p>\n<p>numbers = [10, 20, 30, 40, 50]<\/p>\n<p>index_of_30 = find_index(numbers, 30)<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570<code>find_index<\/code>\uff0c\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u6765\u67e5\u627e\u5217\u8868<code>numbers<\/code>\u4e2d\u76ee\u6807\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5229\u7528\u4e8c\u5206\u67e5\u627e<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6709\u5e8f\u5217\u8868\uff0c\u4e8c\u5206\u67e5\u627e\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u67e5\u627e\u7b97\u6cd5\u3002\u5b83\u901a\u8fc7\u6bcf\u6b21\u5c06\u67e5\u627e\u8303\u56f4\u7f29\u5c0f\u4e00\u534a\uff0c\u5feb\u901f\u627e\u5230\u76ee\u6807\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def binary_search(numbers, target):<\/p>\n<p>    left, right = 0, len(numbers) - 1<\/p>\n<p>    while left &lt;= right:<\/p>\n<p>        mid = (left + right) \/\/ 2<\/p>\n<p>        if numbers[mid] == target:<\/p>\n<p>            return mid<\/p>\n<p>        elif numbers[mid] &lt; target:<\/p>\n<p>            left = mid + 1<\/p>\n<p>        else:<\/p>\n<p>            right = mid - 1<\/p>\n<p>    return -1<\/p>\n<p>numbers = [10, 20, 30, 40, 50]<\/p>\n<p>index_of_30 = binary_search(numbers, 30)<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e8c\u5206\u67e5\u627e\u51fd\u6570<code>binary_search<\/code>\uff0c\u901a\u8fc7\u4e0d\u65ad\u8c03\u6574\u67e5\u627e\u8303\u56f4\uff0c\u5feb\u901f\u627e\u5230\u6709\u5e8f\u5217\u8868<code>numbers<\/code>\u4e2d\u76ee\u6807\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u5229\u7528\u96c6\u5408\u67e5\u627e\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u867d\u7136\u96c6\u5408\u5728Python\u4e2d\u662f\u65e0\u5e8f\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u5c06\u96c6\u5408\u4e0e\u5217\u8868\u7ed3\u5408\u4f7f\u7528\uff0c\u5feb\u901f\u67e5\u627e\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>numbers_set = set(numbers)<\/p>\n<p>target = 30<\/p>\n<p>if target in numbers_set:<\/p>\n<p>    index_of_30 = numbers.index(target)<\/p>\n<p>else:<\/p>\n<p>    index_of_30 = -1<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u96c6\u5408<code>numbers_set<\/code>\uff0c\u7136\u540e\u68c0\u67e5\u76ee\u6807\u6570\u5b57\u662f\u5426\u5728\u96c6\u5408\u4e2d\uff0c\u5982\u679c\u5b58\u5728\u5219\u4f7f\u7528\u5217\u8868\u7684<code>index<\/code>\u65b9\u6cd5\u627e\u5230\u5176\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u5229\u7528\u6392\u5e8f\u548c\u67e5\u627e\u7ed3\u5408\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5148\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u518d\u8fdb\u884c\u67e5\u627e\u64cd\u4f5c\u3002\u53ef\u4ee5\u5c06\u6392\u5e8f\u548c\u67e5\u627e\u7ed3\u5408\u4f7f\u7528\uff0c\u5feb\u901f\u627e\u5230\u76ee\u6807\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [50, 20, 30, 10, 40]<\/p>\n<p>sorted_numbers = sorted(numbers)<\/p>\n<p>index_of_30 = sorted_numbers.index(30)<\/p>\n<p>print(f&quot;Index of 30 in sorted list: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bf9\u5217\u8868<code>numbers<\/code>\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u5230<code>sorted_numbers<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>index<\/code>\u65b9\u6cd5\u627e\u5230\u76ee\u6807\u6570\u5b5730\u5728\u6392\u5e8f\u540e\u7684\u5217\u8868\u4e2d\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u5341\u4e00\u3001\u5229\u7528\u8fed\u4ee3\u5668\u67e5\u627e\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u8fed\u4ee3\u5668\u662f\u4e00\u79cd\u904d\u5386\u5bb9\u5668\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u8fed\u4ee3\u5668\uff0c\u7136\u540e\u8fdb\u884c\u67e5\u627e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>target = 30<\/p>\n<p>index_of_30 = next((i for i, num in enumerate(numbers) if num == target), -1)<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<code>(i for i, num in enumerate(numbers) if num == target)<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u901a\u8fc7<code>next<\/code>\u51fd\u6570\u67e5\u627e\u76ee\u6807\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u5341\u4e8c\u3001\u5229\u7528map\u51fd\u6570\u67e5\u627e\u7d22\u5f15<\/p>\n<\/p>\n<p><p><code>map<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u51fd\u6570\u5e94\u7528\u4e8e\u5e8f\u5217\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408<code>map<\/code>\u51fd\u6570\u548c<code>enumerate<\/code>\u51fd\u6570\u6765\u67e5\u627e\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>target = 30<\/p>\n<p>index_of_30 = list(map(lambda x: x[0], filter(lambda x: x[1] == target, enumerate(numbers))))[0]<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u548c<code>filter<\/code>\u51fd\u6570\u7ed3\u5408\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5305\u542b\u76ee\u6807\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u5341\u4e09\u3001\u5229\u7528\u751f\u6210\u5668\u67e5\u627e\u7d22\u5f15<\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u4e00\u7cfb\u5217\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u6765\u67e5\u627e\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_index_gen(numbers, target):<\/p>\n<p>    for i, num in enumerate(numbers):<\/p>\n<p>        if num == target:<\/p>\n<p>            yield i<\/p>\n<p>numbers = [10, 20, 30, 40, 50]<\/p>\n<p>index_of_30 = next(find_index_gen(numbers, 30), -1)<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570<code>find_index_gen<\/code>\uff0c\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u67e5\u627e\u76ee\u6807\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u5341\u56db\u3001\u5229\u7528\u9012\u5f52\u548c\u4e8c\u5206\u67e5\u627e\u7ed3\u5408\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u9012\u5f52\u548c\u4e8c\u5206\u67e5\u627e\u7ed3\u5408\u4f7f\u7528\uff0c\u521b\u5efa\u4e00\u4e2a\u9ad8\u6548\u7684\u67e5\u627e\u7b97\u6cd5\uff0c\u7528\u4e8e\u67e5\u627e\u6709\u5e8f\u5217\u8868\u4e2d\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_binary_search(numbers, target, left, right):<\/p>\n<p>    if left &gt; right:<\/p>\n<p>        return -1<\/p>\n<p>    mid = (left + right) \/\/ 2<\/p>\n<p>    if numbers[mid] == target:<\/p>\n<p>        return mid<\/p>\n<p>    elif numbers[mid] &lt; target:<\/p>\n<p>        return recursive_binary_search(numbers, target, mid + 1, right)<\/p>\n<p>    else:<\/p>\n<p>        return recursive_binary_search(numbers, target, left, mid - 1)<\/p>\n<p>numbers = [10, 20, 30, 40, 50]<\/p>\n<p>index_of_30 = recursive_binary_search(numbers, 30, 0, len(numbers) - 1)<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u4e8c\u5206\u67e5\u627e\u51fd\u6570<code>recursive_binary_search<\/code>\uff0c\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u67e5\u627e\u6709\u5e8f\u5217\u8868<code>numbers<\/code>\u4e2d\u76ee\u6807\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u5341\u4e94\u3001\u5229\u7528\u5b57\u5178\u548c\u751f\u6210\u5668\u7ed3\u5408\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u5178\u548c\u751f\u6210\u5668\u7ed3\u5408\u4f7f\u7528\uff0c\u521b\u5efa\u4e00\u4e2a\u9ad8\u6548\u7684\u67e5\u627e\u65b9\u6cd5\uff0c\u7528\u4e8e\u67e5\u627e\u5217\u8868\u4e2d\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>index_dict = {num: i for i, num in enumerate(numbers)}<\/p>\n<p>def find_index_gen_dict(target):<\/p>\n<p>    yield index_dict.get(target, -1)<\/p>\n<p>index_of_30 = next(find_index_gen_dict(30))<\/p>\n<p>print(f&quot;Index of 30: {index_of_30}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6570\u5b57\u5230\u7d22\u5f15\u7684\u6620\u5c04<code>index_dict<\/code>\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570<code>find_index_gen_dict<\/code>\uff0c\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u67e5\u627e\u76ee\u6807\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u591a\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5728\u4e0d\u540c\u60c5\u51b5\u4e0b\u67e5\u627e\u5217\u8868\u6216\u6570\u7ec4\u4e2d\u67d0\u4e2a\u6570\u5b57\u7684\u7d22\u5f15\u4f4d\u7f6e\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\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\u4f7f\u7528\u7d22\u5f15\u67e5\u627e\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u7684<code>index()<\/code>\u65b9\u6cd5\u6765\u67e5\u627e\u7279\u5b9a\u6570\u5b57\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5217\u8868<code>numbers = [10, 20, 30, 40]<\/code>\uff0c\u60f3\u8981\u67e5\u627e\u6570\u5b57<code>30<\/code>\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numbers.index(30)<\/code>\uff0c\u8fd4\u56de\u7ed3\u679c\u5c06\u662f<code>2<\/code>\u3002\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u8be5\u6570\u5b57\u5728\u5217\u8868\u4e2d\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u679c\u4e00\u4e2a\u6570\u5b57\u5728\u5217\u8868\u4e2d\u51fa\u73b0\u591a\u6b21\uff0c\u5982\u4f55\u627e\u5230\u6240\u6709\u7684\u7d22\u5f15\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u627e\u5230\u4e00\u4e2a\u6570\u5b57\u5728\u5217\u8868\u4e2d\u6240\u6709\u51fa\u73b0\u4f4d\u7f6e\u7684\u7d22\u5f15\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>numbers = [10, 20, 30, 20, 40]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u627e\u5230\u6570\u5b57<code>20<\/code>\u7684\u6240\u6709\u7d22\u5f15\uff1a<code>[i for i, x in enumerate(numbers) if x == 20]<\/code>\u3002\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5339\u914d\u7d22\u5f15\u7684\u5217\u8868\uff0c\u4f8b\u5982<code>[1, 3]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5b57\u5178\u4e2d\u901a\u8fc7\u952e\u627e\u5230\u5bf9\u5e94\u7684\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u57fa\u4e8e\u952e\u503c\u5bf9\u7684\u96c6\u5408\u3002\u5982\u679c\u60f3\u8981\u901a\u8fc7\u952e\u627e\u5230\u5bf9\u5e94\u7684\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>dict[key]<\/code>\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5b57\u5178<code>data = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/code>\uff0c\u8981\u67e5\u627e\u952e<code>&#39;b&#39;<\/code>\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>data[&#39;b&#39;]<\/code>\uff0c\u7ed3\u679c\u5c06\u662f<code>2<\/code>\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0cPython\u4f1a\u629b\u51fa<code>KeyError<\/code>\u5f02\u5e38\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528<code>dict.get(key)<\/code>\u65b9\u6cd5\u6765\u5b89\u5168\u5730\u83b7\u53d6\u503c\uff0c\u8fd4\u56de<code>None<\/code>\u6216\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\uff0c\u800c\u4e0d\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5229\u7528\u7d22\u5f15\u627e\u5230\u6570\u5b57\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5217\u8868\u7684\u7d22\u5f15\u65b9\u6cd5\u3001\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\u65b9\u6cd5\u3001\u4ee5\u53ca\u901a\u8fc7NumPy [&hellip;]","protected":false},"author":3,"featured_media":1176260,"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\/1176251"}],"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=1176251"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176251\/revisions"}],"predecessor-version":[{"id":1176262,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1176251\/revisions\/1176262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1176260"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1176251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1176251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1176251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}