{"id":1136993,"date":"2025-01-08T21:42:51","date_gmt":"2025-01-08T13:42:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136993.html"},"modified":"2025-01-08T21:42:53","modified_gmt":"2025-01-08T13:42:53","slug":"%e5%9c%a8python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%b1%82%e7%82%b9%e5%88%b0%e7%82%b9%e4%b9%8b%e9%97%b4%e7%9a%84%e8%b7%9d%e7%a6%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1136993.html","title":{"rendered":"\u5728python\u4e2d\u5982\u4f55\u6c42\u70b9\u5230\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100942\/b29e77b5-4449-4b02-914e-4ecd91c93953.webp\" alt=\"\u5728python\u4e2d\u5982\u4f55\u6c42\u70b9\u5230\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6c42\u70b9\u5230\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u516c\u5f0f\u3001\u66fc\u54c8\u987f\u8ddd\u79bb\u6216\u4f7f\u7528\u73b0\u6709\u7684\u5e93\u5982NumPy\u3001Scipy\u7b49<\/strong>\u3002\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u8ba1\u7b97\u4e24\u4e2a\u70b9\u4e4b\u95f4\u7684\u76f4\u7ebf\u8ddd\u79bb\uff0c\u975e\u5e38\u9002\u7528\u4e8e\u4e8c\u7ef4\u548c\u4e09\u7ef4\u7a7a\u95f4\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u4e0d\u540c\u65b9\u6cd5\u8ba1\u7b97\u70b9\u5230\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u516c\u5f0f<\/p>\n<\/p>\n<p><p>\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u516c\u5f0f\u662f\u8ba1\u7b97\u4e24\u4e2a\u70b9\u4e4b\u95f4\u76f4\u7ebf\u8ddd\u79bb\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\u3002\u5728\u4e8c\u7ef4\u7a7a\u95f4\u4e2d\uff0c\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ d = \\sqrt{(x_2 &#8211; x_1)^2 + (y_2 &#8211; y_1)^2} ]<\/p>\n<\/p>\n<p><p>\u5728\u4e09\u7ef4\u7a7a\u95f4\u4e2d\uff0c\u8fd9\u4e2a\u516c\u5f0f\u6269\u5c55\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ d = \\sqrt{(x_2 &#8211; x_1)^2 + (y_2 &#8211; y_1)^2 + (z_2 &#8211; z_1)^2} ]<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u516c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8ba1\u7b97\u4e24\u4e2a\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5927\u591a\u6570\u4e8c\u7ef4\u548c\u4e09\u7ef4\u7a7a\u95f4\u7684\u8ddd\u79bb\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u8ba1\u7b97\u8ddd\u79bb<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u6570\u503c\u8ba1\u7b97\u7684\u5f3a\u5927\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u51fd\u6570\u6765\u8ba1\u7b97\u5411\u91cf\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002\u4e0b\u9762\u662f\u4f7f\u7528NumPy\u8ba1\u7b97\u4e8c\u7ef4\u548c\u4e09\u7ef4\u7a7a\u95f4\u4e2d\u70b9\u5230\u70b9\u8ddd\u79bb\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4e8c\u7ef4\u70b9<\/strong><\/h2>\n<p>point1 = np.array([1, 2])<\/p>\n<p>point2 = np.array([4, 6])<\/p>\n<h2><strong>\u8ba1\u7b97\u8ddd\u79bb<\/strong><\/h2>\n<p>distance = np.linalg.norm(point1 - point2)<\/p>\n<p>print(f&quot;2D\u7a7a\u95f4\u4e2d\u70b9\u5230\u70b9\u7684\u8ddd\u79bb: {distance}&quot;)<\/p>\n<h2><strong>\u4e09\u7ef4\u70b9<\/strong><\/h2>\n<p>point1 = np.array([1, 2, 3])<\/p>\n<p>point2 = np.array([4, 6, 8])<\/p>\n<h2><strong>\u8ba1\u7b97\u8ddd\u79bb<\/strong><\/h2>\n<p>distance = np.linalg.norm(point1 - point2)<\/p>\n<p>print(f&quot;3D\u7a7a\u95f4\u4e2d\u70b9\u5230\u70b9\u7684\u8ddd\u79bb: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Scipy\u8ba1\u7b97\u8ddd\u79bb<\/h3>\n<\/p>\n<p><p>Scipy\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u5305\u542b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\u548c\u5de5\u5177\u3002\u4f7f\u7528Scipy\u7684<code>spatial.distance<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u4e0d\u540c\u7ef4\u5ea6\u7a7a\u95f4\u4e2d\u7684\u70b9\u5230\u70b9\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.spatial import distance<\/p>\n<h2><strong>\u4e8c\u7ef4\u70b9<\/strong><\/h2>\n<p>point1 = [1, 2]<\/p>\n<p>point2 = [4, 6]<\/p>\n<h2><strong>\u8ba1\u7b97\u8ddd\u79bb<\/strong><\/h2>\n<p>dist = distance.euclidean(point1, point2)<\/p>\n<p>print(f&quot;2D\u7a7a\u95f4\u4e2d\u70b9\u5230\u70b9\u7684\u8ddd\u79bb: {dist}&quot;)<\/p>\n<h2><strong>\u4e09\u7ef4\u70b9<\/strong><\/h2>\n<p>point1 = [1, 2, 3]<\/p>\n<p>point2 = [4, 6, 8]<\/p>\n<h2><strong>\u8ba1\u7b97\u8ddd\u79bb<\/strong><\/h2>\n<p>dist = distance.euclidean(point1, point2)<\/p>\n<p>print(f&quot;3D\u7a7a\u95f4\u4e2d\u70b9\u5230\u70b9\u7684\u8ddd\u79bb: {dist}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u8ddd\u79bb\u8ba1\u7b97\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\uff0c\u8fd8\u6709\u5176\u4ed6\u51e0\u79cd\u5e38\u89c1\u7684\u8ddd\u79bb\u8ba1\u7b97\u65b9\u6cd5\uff0c\u5982\u66fc\u54c8\u987f\u8ddd\u79bb\u3001\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u6709\u4e0d\u540c\u7684\u4f7f\u7528\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>1. \u66fc\u54c8\u987f\u8ddd\u79bb<\/h4>\n<\/p>\n<p><p>\u66fc\u54c8\u987f\u8ddd\u79bb\u8ba1\u7b97\u4e24\u4e2a\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u7684\u65b9\u5f0f\u662f\u7d2f\u52a0\u5b83\u4eec\u5728\u5404\u4e2a\u5750\u6807\u8f74\u4e0a\u7684\u7edd\u5bf9\u5dee\u503c\u3002\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ d = |x_2 &#8211; x_1| + |y_2 &#8211; y_1| ]<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8ba1\u7b97\u66fc\u54c8\u987f\u8ddd\u79bb\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def manhattan_distance(point1, point2):<\/p>\n<p>    return sum(abs(a - b) for a, b in zip(point1, point2))<\/p>\n<h2><strong>\u4e8c\u7ef4\u70b9<\/strong><\/h2>\n<p>point1 = [1, 2]<\/p>\n<p>point2 = [4, 6]<\/p>\n<p>print(f&quot;\u66fc\u54c8\u987f\u8ddd\u79bb: {manhattan_distance(point1, point2)}&quot;)<\/p>\n<h2><strong>\u4e09\u7ef4\u70b9<\/strong><\/h2>\n<p>point1 = [1, 2, 3]<\/p>\n<p>point2 = [4, 6, 8]<\/p>\n<p>print(f&quot;\u66fc\u54c8\u987f\u8ddd\u79bb: {manhattan_distance(point1, point2)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5207\u6bd4\u96ea\u592b\u8ddd\u79bb<\/h4>\n<\/p>\n<p><p>\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb\u8ba1\u7b97\u4e24\u4e2a\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u662f\u5b83\u4eec\u5728\u5404\u4e2a\u5750\u6807\u8f74\u4e0a\u7684\u6700\u5927\u5dee\u503c\u3002\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ d = \\max(|x_2 &#8211; x_1|, |y_2 &#8211; y_1|) ]<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8ba1\u7b97\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def chebyshev_distance(point1, point2):<\/p>\n<p>    return max(abs(a - b) for a, b in zip(point1, point2))<\/p>\n<h2><strong>\u4e8c\u7ef4\u70b9<\/strong><\/h2>\n<p>point1 = [1, 2]<\/p>\n<p>point2 = [4, 6]<\/p>\n<p>print(f&quot;\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb: {chebyshev_distance(point1, point2)}&quot;)<\/p>\n<h2><strong>\u4e09\u7ef4\u70b9<\/strong><\/h2>\n<p>point1 = [1, 2, 3]<\/p>\n<p>point2 = [4, 6, 8]<\/p>\n<p>print(f&quot;\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb: {chebyshev_distance(point1, point2)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8ba1\u7b97\u70b9\u5230\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u7b80\u5355\u7684\u6570\u5b66\u516c\u5f0f\u8fd8\u662f\u5229\u7528\u5f3a\u5927\u7684\u5e93\u5982NumPy\u548cScipy\uff0c\u90fd\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8ddd\u79bb\u8ba1\u7b97\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u7684\u51c6\u786e\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p><strong>\u9009\u62e9\u5408\u9002\u7684\u8ddd\u79bb\u8ba1\u7b97\u65b9\u6cd5<\/strong>\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u7279\u6027\u3002<strong>\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb<\/strong>\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0c<strong>\u66fc\u54c8\u987f\u8ddd\u79bb<\/strong>\u5728\u67d0\u4e9b\u7279\u5b9a\u7684\u7f51\u683c\u72b6\u8def\u5f84\u4e2d\u66f4\u4e3a\u6709\u6548\uff0c\u800c<strong>\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb<\/strong>\u5728\u68cb\u76d8\u8def\u5f84\u4e2d\u5c24\u4e3a\u5e38\u7528\u3002\u901a\u8fc7\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u80fd\u591f\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u8ddd\u79bb\u8ba1\u7b97\u95ee\u9898\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\u8ddd\u79bb\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u5b66\u5e93\uff08math\uff09\u4e2d\u7684sqrt\u548cpow\u51fd\u6570\u6765\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002\u7ed9\u5b9a\u4e24\u70b9\u7684\u5750\u6807\uff08x1, y1\uff09\u548c\uff08x2, y2\uff09\uff0c\u4f7f\u7528\u516c\u5f0f\uff1a<br \/>[ \\text{distance} = \\sqrt{(x2 &#8211; x1)^2 + (y2 &#8211; y1)^2} ]<br \/>\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">import math\n\ndef calculate_distance(x1, y1, x2, y2):\n    return math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))\n\ndistance = calculate_distance(1, 2, 4, 6)\nprint(distance)\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u73b0\u6210\u7684\u5e93\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u8ddd\u79bb\uff1f<\/strong><br \/>\u786e\u5b9e\uff0cPython\u4e2d\u6709\u8bb8\u591a\u73b0\u6210\u7684\u5e93\u53ef\u4ee5\u5e2e\u52a9\u8ba1\u7b97\u8ddd\u79bb\uff0c\u4f8b\u5982NumPy\u548cSciPy\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u548c\u6570\u5b66\u8fd0\u7b97\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u591a\u7ef4\u7a7a\u95f4\u4e2d\u7684\u8ddd\u79bb\u3002\u4f8b\u5982\uff0c\u4f7f\u7528NumPy\u7684linalg.norm\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u3002  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\npoint1 = np.array([1, 2])\npoint2 = np.array([4, 6])\ndistance = np.linalg.norm(point2 - point1)\nprint(distance)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u8ba1\u7b97\u591a\u7ef4\u7a7a\u95f4\u4e2d\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\uff1f<\/strong><br \/>\u5728\u591a\u7ef4\u7a7a\u95f4\u4e2d\uff0c\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u53ef\u4ee5\u6269\u5c55\u5230\u4efb\u610f\u7ef4\u5ea6\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u516c\u5f0f\u3002\u5bf9\u4e8e\u4e24\u70b9\u7684\u5750\u6807\uff08x1, y1, z1, &#8230;\uff09\u548c\uff08x2, y2, z2, &#8230;\uff09\uff0c\u8ddd\u79bb\u7684\u8ba1\u7b97\u65b9\u6cd5\u4e3a\uff1a<br \/>[ \\text{distance} = \\sqrt{(x2 &#8211; x1)^2 + (y2 &#8211; y1)^2 + (z2 &#8211; z1)^2 + &#8230;} ]<br \/>\u4f7f\u7528NumPy\u7684linalg.norm\u51fd\u6570\u540c\u6837\u9002\u7528\uff0c\u5b83\u53ef\u4ee5\u5904\u7406\u4efb\u610f\u7ef4\u5ea6\u7684\u6570\u7ec4\u3002\u901a\u8fc7\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u8868\u793a\u6bcf\u4e2a\u70b9\u7684\u5750\u6807\uff0c\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u4efb\u610f\u7ef4\u5ea6\u7684\u8ddd\u79bb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6c42\u70b9\u5230\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u516c\u5f0f\u3001\u66fc\u54c8\u987f\u8ddd\u79bb\u6216\u4f7f\u7528\u73b0\u6709\u7684\u5e93\u5982NumPy\u3001Scipy [&hellip;]","protected":false},"author":3,"featured_media":1137001,"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\/1136993"}],"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=1136993"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136993\/revisions"}],"predecessor-version":[{"id":1137004,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136993\/revisions\/1137004"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1137001"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1136993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1136993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1136993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}