{"id":1032491,"date":"2024-12-31T11:34:09","date_gmt":"2024-12-31T03:34:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1032491.html"},"modified":"2024-12-31T11:34:11","modified_gmt":"2024-12-31T03:34:11","slug":"python%e4%b8%ad%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e5%a6%82%e4%bd%95%e6%9f%a5%e6%89%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1032491.html","title":{"rendered":"python\u4e2d\u4e8c\u7ef4\u6570\u7ec4\u5982\u4f55\u67e5\u627e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/ece68692-f6c3-4ff4-86b2-54bd353cce63.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u4e8c\u7ef4\u6570\u7ec4\u5982\u4f55\u67e5\u627e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u67e5\u627e\u4e8c\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u5185\u7f6e\u51fd\u6570\u4ee5\u53ca\u5e93\u51fd\u6570\u7b49\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\uff1a\u904d\u5386\u6574\u4e2a\u6570\u7ec4\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Pandas\u5e93\u3002<\/strong>\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528\u5faa\u73af\u8fdb\u884c\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u8fdb\u884c\u67e5\u627e\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u7684for\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u627e\u5230\u76ee\u6807\u503c\u7684\u4f4d\u7f6e\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_element(matrix, target):<\/p>\n<p>    for i in range(len(matrix)):<\/p>\n<p>        for j in range(len(matrix[i])):<\/p>\n<p>            if matrix[i][j] == target:<\/p>\n<p>                return (i, j)<\/p>\n<p>    return None<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>target = 5<\/p>\n<p>result = find_element(matrix, target)<\/p>\n<p>print(result)  # \u8f93\u51fa: (1, 1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4e8c\u7ef4\u6570\u7ec4\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u4e8c\u7ef4\u6570\u7ec4\u662f\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u672c\u8eab\u4e5f\u662f\u6570\u7ec4\u7684\u6570\u7ec4\u3002\u5b83\u53ef\u4ee5\u88ab\u770b\u6210\u662f\u4e00\u4e2a\u77e9\u9635\u6216\u8005\u8868\u683c\u7ed3\u6784\u3002\u6bcf\u4e2a\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u4e24\u4e2a\u7d22\u5f15\u6765\u8bbf\u95ee\uff0c\u4e00\u4e2a\u662f\u884c\u7d22\u5f15\uff0c\u4e00\u4e2a\u662f\u5217\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u7ef4\u6570\u7ec4\u5728Python\u4e2d\u53ef\u4ee5\u7528\u5d4c\u5957\u5217\u8868\u6765\u8868\u793a\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u662f\u67e5\u627e\u5143\u7d20\u7684\u57fa\u7840\u64cd\u4f5c\u3002\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684for\u5faa\u73af\u6765\u904d\u5386\u6bcf\u4e00\u4e2a\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(len(matrix)):<\/p>\n<p>    for j in range(len(matrix[i])):<\/p>\n<p>        print(matrix[i][j])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u4f1a\u4f9d\u6b21\u8f93\u51fa\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u67e5\u627e\u5143\u7d20\u7684\u4f4d\u7f6e<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\uff0c\u67e5\u627e\u76ee\u6807\u5143\u7d20\u7684\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_element(matrix, target):<\/p>\n<p>    for i in range(len(matrix)):<\/p>\n<p>        for j in range(len(matrix[i])):<\/p>\n<p>            if matrix[i][j] == target:<\/p>\n<p>                return (i, j)<\/p>\n<p>    return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u76ee\u6807\u5143\u7d20\u5728\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56deNone\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u67e5\u627e<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8868\u8fbe\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u5728\u4e8c\u7ef4\u6570\u7ec4\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u67e5\u627e\u76ee\u6807\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>target = 5<\/p>\n<p>positions = [(i, j) for i in range(len(matrix)) for j in range(len(matrix[i])) if matrix[i][j] == target]<\/p>\n<p>print(positions)  # \u8f93\u51fa: [(1, 1)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u67e5\u627e<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u67e5\u627e\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p>target = 5<\/p>\n<p>result = np.where(matrix == target)<\/p>\n<p>print(result)  # \u8f93\u51fa: (array([1]), array([1]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>np.where<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u884c\u7d22\u5f15\u548c\u5217\u7d22\u5f15\u7684\u5143\u7ec4\uff0c\u8868\u793a\u76ee\u6807\u5143\u7d20\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Pandas\u5e93\u8fdb\u884c\u67e5\u627e<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u6570\u636e\u5206\u6790\u4e2d\u5e38\u7528\u7684\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u529f\u80fd\u3002\u5728\u5904\u7406\u4e8c\u7ef4\u6570\u636e\u65f6\uff0cPandas\u7684DataFrame\u975e\u5e38\u65b9\u4fbf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>df = pd.DataFrame(matrix)<\/p>\n<p>target = 5<\/p>\n<p>result = df.isin([target])<\/p>\n<p>positions = list(zip(*np.where(result)))<\/p>\n<p>print(positions)  # \u8f93\u51fa: [(1, 1)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>df.isin<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14DataFrame\uff0c\u8868\u793a\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u7b49\u4e8e\u76ee\u6807\u503c\u3002\u4f7f\u7528<code>np.where<\/code>\u51fd\u6570\u53ef\u4ee5\u83b7\u53d6\u76ee\u6807\u5143\u7d20\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f18\u5316\u67e5\u627e\u6548\u7387<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f18\u5316\u67e5\u627e\u7b97\u6cd5\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e8c\u7ef4\u6570\u7ec4\u662f\u6709\u5e8f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u6765\u63d0\u9ad8\u67e5\u627e\u6548\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def binary_search(matrix, target):<\/p>\n<p>    rows, cols = len(matrix), len(matrix[0])<\/p>\n<p>    left, right = 0, rows * cols - 1<\/p>\n<p>    while left &lt;= right:<\/p>\n<p>        mid = (left + right) \/\/ 2<\/p>\n<p>        mid_value = matrix[mid \/\/ cols][mid % cols]<\/p>\n<p>        if mid_value == target:<\/p>\n<p>            return (mid \/\/ cols, mid % cols)<\/p>\n<p>        elif mid_value &lt; target:<\/p>\n<p>            left = mid + 1<\/p>\n<p>        else:<\/p>\n<p>            right = mid - 1<\/p>\n<p>    return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u9002\u7528\u4e8e\u884c\u548c\u5217\u90fd\u6309\u5347\u5e8f\u6392\u5e8f\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u5b83\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662fO(log(m * n))\uff0c\u6bd4\u7b80\u5355\u7684\u904d\u5386\u8981\u9ad8\u6548\u5f97\u591a\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u67e5\u627e\u4e8c\u7ef4\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001NumPy\u5e93\u548cPandas\u5e93\u7b49\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5c0f\u89c4\u6a21\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\uff1b\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5efa\u8bae\u4f7f\u7528NumPy\u5e93\u6216Pandas\u5e93\u4ee5\u63d0\u9ad8\u6548\u7387\u3002\u5982\u679c\u6570\u7ec4\u662f\u6709\u5e8f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u8fdb\u4e00\u6b65\u4f18\u5316\u67e5\u627e\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u67e5\u627e\u4e8c\u7ef4\u6570\u7ec4\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\uff0c\u67e5\u627e\u7279\u5b9a\u5143\u7d20\u3002\u901a\u8fc7\u4f7f\u7528<code>for<\/code>\u5faa\u73af\uff0c\u53ef\u4ee5\u9010\u884c\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u3002\u5982\u679c\u627e\u5230\u5339\u914d\u9879\uff0c\u53ef\u4ee5\u7acb\u5373\u8fd4\u56de\u5176\u4f4d\u7f6e\u6216\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\u3002\u6b64\u5916\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u6548\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6570\u7ec4\uff0c\u5982\u679c\u60a8\u7ecf\u5e38\u8fdb\u884c\u6b64\u7c7b\u64cd\u4f5c\uff0c\u8003\u8651\u4f7f\u7528NumPy\u3002<\/p>\n<p><strong>\u5728Python\u7684\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u5982\u4f55\u8fdb\u884c\u6761\u4ef6\u67e5\u627e\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u67e5\u627e\u5143\u7d20\uff0c\u4f8b\u5982\u67e5\u627e\u6240\u6709\u5927\u4e8e\u67d0\u4e2a\u503c\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u901a\u8fc7\u904d\u5386\u6bcf\u4e00\u884c\u548c\u6bcf\u4e00\u5217\uff0c\u53ef\u4ee5\u8f7b\u677e\u6536\u96c6\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002\u6b64\u5916\uff0cNumPy\u4e5f\u5141\u8bb8\u60a8\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u5feb\u901f\u5b9e\u73b0\u6761\u4ef6\u67e5\u627e\uff0c\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u73b0\u6210\u7684\u5e93\u53ef\u4ee5\u7b80\u5316Python\u4e2d\u4e8c\u7ef4\u6570\u7ec4\u7684\u67e5\u627e\u64cd\u4f5c\uff1f<\/strong><br \/>\u786e\u5b9e\u6709\u4e00\u4e9b\u5e93\u53ef\u4ee5\u7b80\u5316\u67e5\u627e\u64cd\u4f5c\u3002\u4f8b\u5982\uff0cNumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u4e13\u4e3a\u6570\u503c\u8ba1\u7b97\u8bbe\u8ba1\uff0c\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u4e30\u5bcc\u7684\u64cd\u4f5c\u529f\u80fd\u3002\u4f7f\u7528NumPy\uff0c\u60a8\u53ef\u4ee5\u5229\u7528\u6570\u7ec4\u7684\u5207\u7247\u548c\u7d22\u5f15\u529f\u80fd\uff0c\u5feb\u901f\u627e\u5230\u6240\u9700\u5143\u7d20\u3002\u6b64\u5916\uff0cPandas\u5e93\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u8868\u683c\u6570\u636e\u4e2d\u7684\u67e5\u627e\u548c\u7b5b\u9009\u4efb\u52a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u67e5\u627e\u4e8c\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5faa\u73af\u3001\u5185\u7f6e\u51fd\u6570\u4ee5\u53ca\u5e93\u51fd\u6570\u7b49\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\uff1a\u904d\u5386\u6574\u4e2a\u6570\u7ec4 [&hellip;]","protected":false},"author":3,"featured_media":1032503,"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\/1032491"}],"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=1032491"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032491\/revisions"}],"predecessor-version":[{"id":1032505,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032491\/revisions\/1032505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1032503"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1032491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1032491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1032491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}