{"id":1048243,"date":"2024-12-31T13:49:22","date_gmt":"2024-12-31T05:49:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1048243.html"},"modified":"2024-12-31T13:49:24","modified_gmt":"2024-12-31T05:49:24","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e4%b8%89%e7%bb%b4%e6%95%b0%e7%bb%84%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1048243.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u4e09\u7ef4\u6570\u7ec4\u6392\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/356ccc9d-c060-4735-b934-cd2961fbaf73.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u4e09\u7ef4\u6570\u7ec4\u6392\u5e8f\" \/><\/p>\n<p><p> <strong>Python\u5b9e\u73b0\u4e09\u7ef4\u6570\u7ec4\u6392\u5e8f\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u6392\u5e8f\u51fd\u6570\u3001\u4f7f\u7528\u591a\u91cd\u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/strong>\u3002  <\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528NumPy\u5e93<\/strong>\u662f\u6700\u4e3a\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3aNumPy\u5e93\u4e3a\u591a\u7ef4\u6570\u7ec4\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\u548c\u4fbf\u6377\u7684\u64cd\u4f5c\u529f\u80fd\u3002NumPy\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u4e09\u7ef4\u6570\u7ec4\uff0c\u8fd8\u80fd\u591f\u5bf9\u5176\u8fdb\u884c\u591a\u79cd\u64cd\u4f5c\u548c\u6392\u5e8f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u6765\u5b9e\u73b0\u4e09\u7ef4\u6570\u7ec4\u7684\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><h4>1. \u521b\u5efa\u4e09\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\u3002\u4f7f\u7528NumPy\u5e93\u4e2d\u7684<code>numpy.array()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_3d = np.array([[[4, 5, 6], [3, 2, 1], [9, 8, 7]],<\/p>\n<p>                     [[1, 3, 5], [2, 4, 6], [7, 9, 8]],<\/p>\n<p>                     [[9, 7, 5], [6, 4, 2], [3, 1, 0]]])<\/p>\n<p>print(&quot;Original 3D array:\\n&quot;, array_3d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bf9\u4e09\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u4e2d\u7684<code>numpy.sort()<\/code>\u51fd\u6570\u5bf9\u4e09\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>numpy.sort()<\/code>\u51fd\u6570\u53ef\u4ee5\u6307\u5b9a\u6392\u5e8f\u7684\u8f74\uff08axis\uff09\uff0c\u8f740\u662f\u6309\u884c\u6392\u5e8f\uff0c\u8f741\u662f\u6309\u5217\u6392\u5e8f\uff0c\u8f742\u662f\u6309\u6700\u5185\u5c42\u7684\u5143\u7d20\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bf9\u4e09\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>sorted_array_3d = np.sort(array_3d, axis=2)<\/p>\n<p>print(&quot;Sorted 3D array along the innermost axis:\\n&quot;, sorted_array_3d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u81ea\u5b9a\u4e49\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u4e2d\u7684<code>numpy.argsort()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u6392\u5e8f\u540e\u7684\u7d22\u5f15\uff0c\u5e76\u6839\u636e\u8fd9\u4e9b\u7d22\u5f15\u91cd\u65b0\u6392\u5217\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u6392\u5e8f\u540e\u7684\u7d22\u5f15<\/p>\n<p>sorted_indices = np.argsort(array_3d, axis=2)<\/p>\n<h2><strong>\u6839\u636e\u6392\u5e8f\u540e\u7684\u7d22\u5f15\u91cd\u65b0\u6392\u5217\u6570\u7ec4<\/strong><\/h2>\n<p>sorted_array_3d_custom = np.take_along_axis(array_3d, sorted_indices, axis=2)<\/p>\n<p>print(&quot;Custom sorted 3D array along the innermost axis:\\n&quot;, sorted_array_3d_custom)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6392\u5e8f\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528NumPy\u5e93\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7Python\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u6765\u5b9e\u73b0\u4e09\u7ef4\u6570\u7ec4\u7684\u6392\u5e8f\u3002\u6b64\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u4f7f\u7528\u5916\u90e8\u5e93\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u4e09\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array_3d = [[[4, 5, 6], [3, 2, 1], [9, 8, 7]],<\/p>\n<p>            [[1, 3, 5], [2, 4, 6], [7, 9, 8]],<\/p>\n<p>            [[9, 7, 5], [6, 4, 2], [3, 1, 0]]]<\/p>\n<p>print(&quot;Original 3D array:\\n&quot;, array_3d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bf9\u4e09\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u548c<code>map()<\/code>\u51fd\u6570\u5bf9\u4e09\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bf9\u6700\u5185\u5c42\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>sorted_array_3d = [[[sorted(inner) for inner in outer] for outer in layer] for layer in array_3d]<\/p>\n<p>print(&quot;Sorted 3D array:\\n&quot;, sorted_array_3d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u591a\u91cd\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u591a\u91cd\u5faa\u73af\u7684\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u4e09\u7ef4\u6570\u7ec4\u7684\u6392\u5e8f\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5bf9\u6bcf\u4e2a\u7ef4\u5ea6\u8fdb\u884c\u4e0d\u540c\u7684\u6392\u5e8f\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u4e09\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array_3d = [[[4, 5, 6], [3, 2, 1], [9, 8, 7]],<\/p>\n<p>            [[1, 3, 5], [2, 4, 6], [7, 9, 8]],<\/p>\n<p>            [[9, 7, 5], [6, 4, 2], [3, 1, 0]]]<\/p>\n<p>print(&quot;Original 3D array:\\n&quot;, array_3d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bf9\u4e09\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u591a\u91cd\u5faa\u73af\u5bf9\u4e09\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bf9\u6700\u5185\u5c42\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>for layer in array_3d:<\/p>\n<p>    for outer in layer:<\/p>\n<p>        outer.sort()<\/p>\n<p>print(&quot;Sorted 3D array using loops:\\n&quot;, array_3d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u548c\u6392\u5e8f\u6570\u7ec4\u3002\u5b83\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u6570\u7ec4\u8fdb\u884c\u7b80\u5355\u6392\u5e8f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u4e09\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array_3d = [[[4, 5, 6], [3, 2, 1], [9, 8, 7]],<\/p>\n<p>            [[1, 3, 5], [2, 4, 6], [7, 9, 8]],<\/p>\n<p>            [[9, 7, 5], [6, 4, 2], [3, 1, 0]]]<\/p>\n<p>print(&quot;Original 3D array:\\n&quot;, array_3d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bf9\u4e09\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5bf9\u4e09\u7ef4\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bf9\u6700\u5185\u5c42\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f<\/p>\n<p>sorted_array_3d = [[[sorted(inner) for inner in outer] for outer in layer] for layer in array_3d]<\/p>\n<p>print(&quot;Sorted 3D array using list comprehension:\\n&quot;, sorted_array_3d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u591a\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u4e09\u7ef4\u6570\u7ec4\u7684\u6392\u5e8f\u3002<strong>\u4f7f\u7528NumPy\u5e93<\/strong>\u662f\u6700\u4e3a\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u4fbf\u6377\u7684\u64cd\u4f5c\u3002\u540c\u65f6\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u4e5f\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u3001\u591a\u91cd\u5faa\u73af\u6216\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u6700\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e09\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u4e09\u7ef4\u6570\u7ec4\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip install numpy<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u521b\u5efa\u4e09\u7ef4\u6570\u7ec4\u7684\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u5f62\u72b6\u4e3a(2, 3, 4)\u7684\u4e09\u7ef4\u6570\u7ec4\narray_3d = np.random.rand(2, 3, 4)\nprint(array_3d)\n<\/code><\/pre>\n<p><strong>\u5728\u6392\u5e8f\u4e09\u7ef4\u6570\u7ec4\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u6392\u5e8f\u7684\u7ef4\u5ea6\uff1f<\/strong><br \/>\u5728\u6392\u5e8f\u4e09\u7ef4\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u6392\u5e8f\u7684\u7ef4\u5ea6\u3002NumPy\u7684<code>np.sort()<\/code>\u51fd\u6570\u5141\u8bb8\u6307\u5b9a<code>axis<\/code>\u53c2\u6570\uff0c\u4ee5\u786e\u5b9a\u5728\u54ea\u4e2a\u7ef4\u5ea6\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c<code>axis=0<\/code>\u8868\u793a\u6cbf\u7740\u7b2c\u4e00\u4e2a\u7ef4\u5ea6\u6392\u5e8f\uff0c\u800c<code>axis=1<\/code>\u8868\u793a\u6cbf\u7740\u7b2c\u4e8c\u4e2a\u7ef4\u5ea6\u6392\u5e8f\u3002\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">sorted_array = np.sort(array_3d, axis=1)\nprint(sorted_array)\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5bf9\u4e09\u7ef4\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u4e0d\u6539\u53d8\u6574\u4e2a\u6570\u7ec4\u7684\u7ed3\u6784\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u9009\u62e9\u7279\u5b9a\u7684\u5207\u7247\u5bf9\u4e09\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u4e0d\u6539\u53d8\u6574\u4e2a\u6570\u7ec4\u7684\u7ed3\u6784\u3002\u4f7f\u7528\u5207\u7247\u7d22\u5f15\u53ef\u4ee5\u8bbf\u95ee\u7279\u5b9a\u7684\u90e8\u5206\uff0c\u5e76\u4f7f\u7528<code>np.sort()<\/code>\u8fdb\u884c\u6392\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\"># \u5bf9\u7279\u5b9a\u5207\u7247\u8fdb\u884c\u6392\u5e8f\nslice_sorted = np.sort(array_3d[0], axis=1)\nprint(slice_sorted)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u548c\u6392\u5e8f\u4e09\u7ef4\u6570\u7ec4\u4e2d\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9e\u73b0\u4e09\u7ef4\u6570\u7ec4\u6392\u5e8f\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u6392\u5e8f\u51fd\u6570\u3001\u4f7f\u7528\u591a\u91cd\u5faa\u73af\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1048248,"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\/1048243"}],"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=1048243"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1048243\/revisions"}],"predecessor-version":[{"id":1048249,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1048243\/revisions\/1048249"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1048248"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1048243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1048243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1048243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}