{"id":1104202,"date":"2025-01-08T16:18:31","date_gmt":"2025-01-08T08:18:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104202.html"},"modified":"2025-01-08T16:18:52","modified_gmt":"2025-01-08T08:18:52","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e4%b8%a4%e7%82%b9%e7%89%b9%e5%ae%9a%e9%95%bf%e8%b7%af%e5%be%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104202.html","title":{"rendered":"Python\u5982\u4f55\u6c42\u4e24\u70b9\u7279\u5b9a\u957f\u8def\u5f84"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065656\/164aaa4d-b381-4a09-b76f-d84afd13ece5.webp\" alt=\"Python\u5982\u4f55\u6c42\u4e24\u70b9\u7279\u5b9a\u957f\u8def\u5f84\" \/><\/p>\n<p><p> <strong>Python\u6c42\u4e24\u70b9\u7279\u5b9a\u957f\u8def\u5f84\u7684\u65b9\u6cd5\u6709\uff1a\u8ba1\u7b97\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u3001\u4f7f\u7528\u66fc\u54c8\u987f\u8ddd\u79bb\u3001\u4f7f\u7528\u7f51\u7edc\u56fe\u7b97\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u8ba1\u7b97\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u662f\u4e00\u79cd\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u5b66\u516c\u5f0f\u8ba1\u7b97\u4e24\u70b9\u95f4\u7684\u76f4\u7ebf\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><p><strong>\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb<\/strong>\u662f\u51e0\u4f55\u5b66\u4e2d\u6700\u5e38\u7528\u7684\u4e00\u79cd\u8ddd\u79bb\u5ea6\u91cf\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u4e8c\u7ef4\u6216\u591a\u7ef4\u7a7a\u95f4\u4e2d\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u76f4\u7ebf\u8ddd\u79bb\u3002\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u7684\u8ba1\u7b97\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>[<\/p>\n<p>d = \\sqrt{(x_2 &#8211; x_1)^2 + (y_2 &#8211; y_1)^2}<\/p>\n<p>]<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528math\u5e93\u4e2d\u7684sqrt\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e2a\u516c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def euclidean_distance(point1, point2):<\/p>\n<p>    return math.sqrt((point2[0] - point1[0])&lt;strong&gt;2 + (point2[1] - point1[1])&lt;\/strong&gt;2)<\/p>\n<p>point1 = (1, 2)<\/p>\n<p>point2 = (4, 6)<\/p>\n<p>distance = euclidean_distance(point1, point2)<\/p>\n<p>print(f&quot;Euclidean distance: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u6c42\u4e24\u70b9\u7279\u5b9a\u957f\u8def\u5f84\u7684\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb<\/h2>\n<\/p>\n<p><p><strong>\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb<\/strong>\u4e0d\u4ec5\u53ef\u4ee5\u5728\u4e8c\u7ef4\u7a7a\u95f4\u4e2d\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u5728\u4e09\u7ef4\u6216\u66f4\u9ad8\u7ef4\u7684\u7a7a\u95f4\u4e2d\u4f7f\u7528\u3002\u901a\u8fc7\u6269\u5c55\u516c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u591a\u7ef4\u7a7a\u95f4\u4e2d\u7684\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><h3>\u591a\u7ef4\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ef4\u7a7a\u95f4\u4e2d\uff0c\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u516c\u5f0f\u4e3a\uff1a<\/p>\n<p>[<\/p>\n<p>d = \\sqrt{\\sum_{i=1}^{n}(x_{2i} &#8211; x_{1i})^2}<\/p>\n<p>]<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def euclidean_distance_nd(point1, point2):<\/p>\n<p>    return math.sqrt(sum((point2[i] - point1[i])2 for i in range(len(point1))))<\/p>\n<p>point1 = (1, 2, 3)<\/p>\n<p>point2 = (4, 6, 8)<\/p>\n<p>distance = euclidean_distance_nd(point1, point2)<\/p>\n<p>print(f&quot;Euclidean distance in 3D: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528NumPy\u8ba1\u7b97\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u8fd0\u7b97\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u3002\u4f7f\u7528NumPy\u7684linalg.norm\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u6c42\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def euclidean_distance_np(point1, point2):<\/p>\n<p>    return np.linalg.norm(np.array(point2) - np.array(point1))<\/p>\n<p>point1 = np.array([1, 2])<\/p>\n<p>point2 = np.array([4, 6])<\/p>\n<p>distance = euclidean_distance_np(point1, point2)<\/p>\n<p>print(f&quot;Euclidean distance using NumPy: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u66fc\u54c8\u987f\u8ddd\u79bb<\/h2>\n<\/p>\n<p><p><strong>\u66fc\u54c8\u987f\u8ddd\u79bb<\/strong>\u662f\u4e00\u79cd\u4e0d\u540c\u4e8e\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u7684\u5ea6\u91cf\u65b9\u5f0f\uff0c\u5b83\u8ba1\u7b97\u7684\u662f\u4e24\u70b9\u5728\u5404\u4e2a\u7ef4\u5ea6\u4e0a\u7684\u7edd\u5bf9\u8ddd\u79bb\u4e4b\u548c\u3002\u5728\u7f51\u683c\u72b6\u7684\u8def\u5f84\u89c4\u5212\u4e2d\uff0c\u66fc\u54c8\u987f\u8ddd\u79bb\u66f4\u4e3a\u5e38\u7528\u3002<\/p>\n<\/p>\n<p><p>\u66fc\u54c8\u987f\u8ddd\u79bb\u516c\u5f0f\u4e3a\uff1a<\/p>\n<p>[<\/p>\n<p>d = \\sum_{i=1}^{n} |x_{2i} &#8211; x_{1i}|<\/p>\n<p>]<\/p>\n<\/p>\n<p><h3>\u8ba1\u7b97\u66fc\u54c8\u987f\u8ddd\u79bb<\/h3>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def manhattan_distance(point1, point2):<\/p>\n<p>    return sum(abs(point2[i] - point1[i]) for i in range(len(point1)))<\/p>\n<p>point1 = (1, 2)<\/p>\n<p>point2 = (4, 6)<\/p>\n<p>distance = manhattan_distance(point1, point2)<\/p>\n<p>print(f&quot;Manhattan distance: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u591a\u7ef4\u66fc\u54c8\u987f\u8ddd\u79bb<\/h3>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u66fc\u54c8\u987f\u8ddd\u79bb\u4e5f\u53ef\u4ee5\u6269\u5c55\u5230\u591a\u7ef4\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def manhattan_distance_nd(point1, point2):<\/p>\n<p>    return sum(abs(point2[i] - point1[i]) for i in range(len(point1)))<\/p>\n<p>point1 = (1, 2, 3)<\/p>\n<p>point2 = (4, 6, 8)<\/p>\n<p>distance = manhattan_distance_nd(point1, point2)<\/p>\n<p>print(f&quot;Manhattan distance in 3D: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u7f51\u7edc\u56fe\u7b97\u6cd5<\/h2>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u5982\u5730\u56fe\u5bfc\u822a\u3001\u8def\u5f84\u89c4\u5212\u7b49\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u7f51\u7edc\u56fe\u7b97\u6cd5\u6765\u6c42\u89e3\u4e24\u70b9\u95f4\u7684\u7279\u5b9a\u8def\u5f84\u3002\u5e38\u7528\u7684\u7f51\u7edc\u56fe\u7b97\u6cd5\u5305\u62ec<strong>Dijkstra\u7b97\u6cd5<\/strong>\u3001<strong>A*\u7b97\u6cd5<\/strong>\u548c<strong>Floyd-Warshall\u7b97\u6cd5<\/strong>\u7b49\u3002<\/p>\n<\/p>\n<p><h3>Dijkstra\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>Dijkstra\u7b97\u6cd5\u662f\u4e00\u79cd\u7ecf\u5178\u7684\u5355\u6e90\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\uff0c\u9002\u7528\u4e8e\u65e0\u8d1f\u6743\u56fe\u3002\u5b83\u901a\u8fc7\u8d2a\u5fc3\u7b56\u7565\u9010\u6b65\u6269\u5c55\u6700\u77ed\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import heapq<\/p>\n<p>def dijkstra(graph, start, end):<\/p>\n<p>    queue = [(0, start)]<\/p>\n<p>    distances = {vertex: float(&#39;inf&#39;) for vertex in graph}<\/p>\n<p>    distances[start] = 0<\/p>\n<p>    while queue:<\/p>\n<p>        current_distance, current_vertex = heapq.heappop(queue)<\/p>\n<p>        if current_distance &gt; distances[current_vertex]:<\/p>\n<p>            continue<\/p>\n<p>        for neighbor, weight in graph[current_vertex].items():<\/p>\n<p>            distance = current_distance + weight<\/p>\n<p>            if distance &lt; distances[neighbor]:<\/p>\n<p>                distances[neighbor] = distance<\/p>\n<p>                heapq.heappush(queue, (distance, neighbor))<\/p>\n<p>    return distances[end]<\/p>\n<p>graph = {<\/p>\n<p>    &#39;A&#39;: {&#39;B&#39;: 1, &#39;C&#39;: 4},<\/p>\n<p>    &#39;B&#39;: {&#39;A&#39;: 1, &#39;C&#39;: 2, &#39;D&#39;: 5},<\/p>\n<p>    &#39;C&#39;: {&#39;A&#39;: 4, &#39;B&#39;: 2, &#39;D&#39;: 1},<\/p>\n<p>    &#39;D&#39;: {&#39;B&#39;: 5, &#39;C&#39;: 1}<\/p>\n<p>}<\/p>\n<p>start = &#39;A&#39;<\/p>\n<p>end = &#39;D&#39;<\/p>\n<p>distance = dijkstra(graph, start, end)<\/p>\n<p>print(f&quot;Dijkstra distance from {start} to {end}: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>A*\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>A*\u7b97\u6cd5\u662f\u4e00\u79cd\u542f\u53d1\u5f0f\u641c\u7d22\u7b97\u6cd5\uff0c\u901a\u8fc7\u7ed3\u5408\u542f\u53d1\u5f0f\u51fd\u6570\u548c\u8def\u5f84\u6210\u672c\u51fd\u6570\u6765\u627e\u5230\u4e24\u70b9\u95f4\u7684\u6700\u77ed\u8def\u5f84\u3002\u5b83\u901a\u5e38\u6bd4Dijkstra\u7b97\u6cd5\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import heapq<\/p>\n<p>def heuristic(a, b):<\/p>\n<p>    return abs(a[0] - b[0]) + abs(a[1] - b[1])<\/p>\n<p>def a_star(graph, start, end):<\/p>\n<p>    queue = [(0, start)]<\/p>\n<p>    g_scores = {start: 0}<\/p>\n<p>    f_scores = {start: heuristic(start, end)}<\/p>\n<p>    while queue:<\/p>\n<p>        current_f, current = heapq.heappop(queue)<\/p>\n<p>        if current == end:<\/p>\n<p>            return g_scores[current]<\/p>\n<p>        for neighbor, weight in graph[current].items():<\/p>\n<p>            tentative_g_score = g_scores[current] + weight<\/p>\n<p>            if neighbor not in g_scores or tentative_g_score &lt; g_scores[neighbor]:<\/p>\n<p>                g_scores[neighbor] = tentative_g_score<\/p>\n<p>                f_scores[neighbor] = tentative_g_score + heuristic(neighbor, end)<\/p>\n<p>                heapq.heappush(queue, (f_scores[neighbor], neighbor))<\/p>\n<p>    return float(&#39;inf&#39;)<\/p>\n<p>graph = {<\/p>\n<p>    (0, 0): {(1, 0): 1, (0, 1): 1},<\/p>\n<p>    (1, 0): {(0, 0): 1, (1, 1): 1},<\/p>\n<p>    (0, 1): {(0, 0): 1, (1, 1): 1},<\/p>\n<p>    (1, 1): {(1, 0): 1, (0, 1): 1}<\/p>\n<p>}<\/p>\n<p>start = (0, 0)<\/p>\n<p>end = (1, 1)<\/p>\n<p>distance = a_star(graph, start, end)<\/p>\n<p>print(f&quot;A* distance from {start} to {end}: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>Floyd-Warshall\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>Floyd-Warshall\u7b97\u6cd5\u662f\u4e00\u79cd\u591a\u6e90\u6700\u77ed\u8def\u5f84\u7b97\u6cd5\uff0c\u9002\u7528\u4e8e\u4efb\u610f\u6743\u91cd\u7684\u56fe\u3002\u5b83\u901a\u8fc7\u52a8\u6001\u89c4\u5212\u7684\u65b9\u5f0f\u8ba1\u7b97\u6240\u6709\u8282\u70b9\u5bf9\u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def floyd_warshall(graph):<\/p>\n<p>    nodes = list(graph.keys())<\/p>\n<p>    distances = {node: {node: float(&#39;inf&#39;) for node in nodes} for node in nodes}<\/p>\n<p>    for node in nodes:<\/p>\n<p>        distances[node][node] = 0<\/p>\n<p>    for start, neighbors in graph.items():<\/p>\n<p>        for neighbor, weight in neighbors.items():<\/p>\n<p>            distances[start][neighbor] = weight<\/p>\n<p>    for k in nodes:<\/p>\n<p>        for i in nodes:<\/p>\n<p>            for j in nodes:<\/p>\n<p>                if distances[i][j] &gt; distances[i][k] + distances[k][j]:<\/p>\n<p>                    distances[i][j] = distances[i][k] + distances[k][j]<\/p>\n<p>    return distances<\/p>\n<p>graph = {<\/p>\n<p>    &#39;A&#39;: {&#39;B&#39;: 1, &#39;C&#39;: 4},<\/p>\n<p>    &#39;B&#39;: {&#39;A&#39;: 1, &#39;C&#39;: 2, &#39;D&#39;: 5},<\/p>\n<p>    &#39;C&#39;: {&#39;A&#39;: 4, &#39;B&#39;: 2, &#39;D&#39;: 1},<\/p>\n<p>    &#39;D&#39;: {&#39;B&#39;: 5, &#39;C&#39;: 1}<\/p>\n<p>}<\/p>\n<p>distances = floyd_warshall(graph)<\/p>\n<p>start = &#39;A&#39;<\/p>\n<p>end = &#39;D&#39;<\/p>\n<p>distance = distances[start][end]<\/p>\n<p>print(f&quot;Floyd-Warshall distance from {start} to {end}: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u6c42\u89e3\u4e24\u70b9\u95f4\u7684\u7279\u5b9a\u957f\u8def\u5f84\u3002\u6839\u636e\u5b9e\u9645\u5e94\u7528\u573a\u666f\u7684\u4e0d\u540c\uff0c\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u7684\u7b97\u6cd5\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5176\u4ed6\u8ddd\u79bb\u5ea6\u91cf\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u548c\u66fc\u54c8\u987f\u8ddd\u79bb\uff0c<strong>\u8fd8\u6709\u5176\u4ed6\u5e38\u7528\u7684\u8ddd\u79bb\u5ea6\u91cf\u65b9\u6cd5<\/strong>\uff0c\u5982\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb\u3001\u6c49\u660e\u8ddd\u79bb\u7b49\u3002\u8fd9\u4e9b\u8ddd\u79bb\u5ea6\u91cf\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb<\/h3>\n<\/p>\n<p><p>\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb\u7528\u4e8e\u5ea6\u91cf\u68cb\u76d8\u4e0a\u7684\u79fb\u52a8\u8ddd\u79bb\uff0c\u5b83\u8ba1\u7b97\u7684\u662f\u5404\u7ef4\u5ea6\u4e0a\u5750\u6807\u5dee\u7684\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><p>\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb\u516c\u5f0f\u4e3a\uff1a<\/p>\n<p>[<\/p>\n<p>d = \\max_{i=1}^{n} |x_{2i} &#8211; x_{1i}|<\/p>\n<p>]<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def chebyshev_distance(point1, point2):<\/p>\n<p>    return max(abs(point2[i] - point1[i]) for i in range(len(point1)))<\/p>\n<p>point1 = (1, 2)<\/p>\n<p>point2 = (4, 6)<\/p>\n<p>distance = chebyshev_distance(point1, point2)<\/p>\n<p>print(f&quot;Chebyshev distance: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6c49\u660e\u8ddd\u79bb<\/h3>\n<\/p>\n<p><p>\u6c49\u660e\u8ddd\u79bb\u7528\u4e8e\u5ea6\u91cf\u4e24\u4e2a\u5b57\u7b26\u4e32\u6216\u5e8f\u5217\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u5b83\u8ba1\u7b97\u7684\u662f\u5bf9\u5e94\u4f4d\u7f6e\u4e0a\u4e0d\u540c\u5b57\u7b26\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u6c49\u660e\u8ddd\u79bb\u516c\u5f0f\u4e3a\uff1a<\/p>\n<p>[<\/p>\n<p>d = \\sum_{i=1}^{n} [x_{1i} \\ne x_{2i}]<\/p>\n<p>]<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def hamming_distance(str1, str2):<\/p>\n<p>    if len(str1) != len(str2):<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Strings must be of the same length&quot;)<\/p>\n<p>    return sum(c1 != c2 for c1, c2 in zip(str1, str2))<\/p>\n<p>str1 = &quot;karolin&quot;<\/p>\n<p>str2 = &quot;kathrin&quot;<\/p>\n<p>distance = hamming_distance(str1, str2)<\/p>\n<p>print(f&quot;Hamming distance: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f59\u5f26\u76f8\u4f3c\u5ea6<\/h3>\n<\/p>\n<p><p>\u4f59\u5f26\u76f8\u4f3c\u5ea6\u7528\u4e8e\u5ea6\u91cf\u4e24\u4e2a\u5411\u91cf\u4e4b\u95f4\u7684\u5939\u89d2\u4f59\u5f26\u503c\uff0c\u5e38\u7528\u4e8e\u6587\u672c\u76f8\u4f3c\u5ea6\u8ba1\u7b97\u3002\u5176\u503c\u5728-1\u52301\u4e4b\u95f4\uff0c\u503c\u8d8a\u5927\u8868\u793a\u8d8a\u76f8\u4f3c\u3002<\/p>\n<\/p>\n<p><p>\u4f59\u5f26\u76f8\u4f3c\u5ea6\u516c\u5f0f\u4e3a\uff1a<\/p>\n<p>[<\/p>\n<p>\\cos(\\theta) = \\frac{\\vec{A} \\cdot \\vec{B}}{||\\vec{A}|| \\cdot ||\\vec{B}||}<\/p>\n<p>]<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def cosine_similarity(vec1, vec2):<\/p>\n<p>    dot_product = np.dot(vec1, vec2)<\/p>\n<p>    norm_vec1 = np.linalg.norm(vec1)<\/p>\n<p>    norm_vec2 = np.linalg.norm(vec2)<\/p>\n<p>    return dot_product \/ (norm_vec1 * norm_vec2)<\/p>\n<p>vec1 = np.array([1, 2, 3])<\/p>\n<p>vec2 = np.array([4, 5, 6])<\/p>\n<p>similarity = cosine_similarity(vec1, vec2)<\/p>\n<p>print(f&quot;Cosine similarity: {similarity}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u8ddd\u79bb\u5ea6\u91cf\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u6c42\u89e3\u4e24\u70b9\u95f4\u7684\u7279\u5b9a\u957f\u8def\u5f84\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u5f80\u5f80\u80fd\u83b7\u5f97\u66f4\u597d\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u7279\u5b9a\u957f\u5ea6\u8def\u5f84\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u56fe\u8bba\u7b97\u6cd5\u6765\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u7279\u5b9a\u957f\u5ea6\u8def\u5f84\u3002\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5229\u7528Dijkstra\u7b97\u6cd5\u6216A*\u7b97\u6cd5\uff0c\u7ed3\u5408\u8fb9\u7684\u6743\u91cd\u6765\u9650\u5236\u8def\u5f84\u957f\u5ea6\u3002\u53ef\u4ee5\u4f7f\u7528NetworkX\u5e93\u6765\u6784\u5efa\u56fe\u548c\u8ba1\u7b97\u8def\u5f84\uff0c\u786e\u4fdd\u9009\u62e9\u7684\u8def\u5f84\u7b26\u5408\u7279\u5b9a\u957f\u5ea6\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u5b9e\u73b0\u8def\u5f84\u8ba1\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0cNetworkX\u662f\u6700\u5e38\u7528\u7684\u56fe\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\uff0c\u9002\u5408\u7528\u4e8e\u8def\u5f84\u8ba1\u7b97\u3002\u6b64\u5916\uff0cScipy\u5e93\u4e2d\u7684\u7a7a\u95f4\u6570\u636e\u5904\u7406\u529f\u80fd\u4e5f\u53ef\u4ee5\u7528\u6765\u5904\u7406\u8def\u5f84\u548c\u8ddd\u79bb\u8ba1\u7b97\u3002\u6839\u636e\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5b9e\u73b0\u7279\u5b9a\u7684\u8def\u5f84\u8ba1\u7b97\u529f\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8def\u5f84\u8ba1\u7b97\u4e2d\u7684\u6743\u91cd\u548c\u7ea6\u675f\u6761\u4ef6\uff1f<\/strong><br \/>\u5728\u8def\u5f84\u8ba1\u7b97\u4e2d\uff0c\u8fb9\u7684\u6743\u91cd\u901a\u5e38\u4ee3\u8868\u4e86\u8ddd\u79bb\u3001\u65f6\u95f4\u6216\u6210\u672c\u7b49\u56e0\u7d20\u3002\u53ef\u4ee5\u5728\u6784\u5efa\u56fe\u65f6\u4e3a\u6bcf\u6761\u8fb9\u6307\u5b9a\u6743\u91cd\uff0c\u4f7f\u7528\u56fe\u7b97\u6cd5\u65f6\u52a0\u5165\u7ea6\u675f\u6761\u4ef6\uff0c\u5982\u8def\u5f84\u957f\u5ea6\u9650\u5236\u3001\u7ecf\u8fc7\u7279\u5b9a\u70b9\u7b49\u3002\u901a\u8fc7\u8c03\u6574\u7b97\u6cd5\u7684\u53c2\u6570\u548c\u56fe\u7684\u7ed3\u6784\uff0c\u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u7684\u8def\u5f84\u8ba1\u7b97\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6c42\u4e24\u70b9\u7279\u5b9a\u957f\u8def\u5f84\u7684\u65b9\u6cd5\u6709\uff1a\u8ba1\u7b97\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u3001\u4f7f\u7528\u66fc\u54c8\u987f\u8ddd\u79bb\u3001\u4f7f\u7528\u7f51\u7edc\u56fe\u7b97\u6cd5\u3002 \u5176\u4e2d\uff0c\u8ba1\u7b97\u6b27\u51e0\u91cc\u5f97\u8ddd [&hellip;]","protected":false},"author":3,"featured_media":1104233,"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\/1104202"}],"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=1104202"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104202\/revisions"}],"predecessor-version":[{"id":1104235,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104202\/revisions\/1104235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104233"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}