{"id":1069585,"date":"2025-01-08T10:50:17","date_gmt":"2025-01-08T02:50:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069585.html"},"modified":"2025-01-08T10:50:20","modified_gmt":"2025-01-08T02:50:20","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e4%ba%8c%e7%bb%b4%e5%88%97%e8%a1%a8%e7%bf%bb%e8%bd%ac-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069585.html","title":{"rendered":"python\u5982\u4f55\u628a\u4e8c\u7ef4\u5217\u8868\u7ffb\u8f6c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100557\/b20f45d9-4b8f-4632-bfa4-1460f6d0266b.webp\" alt=\"python\u5982\u4f55\u628a\u4e8c\u7ef4\u5217\u8868\u7ffb\u8f6c\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u628a\u4e8c\u7ef4\u5217\u8868\u7ffb\u8f6c\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip()\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528numpy\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip()\uff0c\u56e0\u4e3a\u5b83\u7b80\u6d01\u4e14\u9ad8\u6548\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u53ca\u5176\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip()\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip()\u662f\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868\u7684\u6700\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u884c\u548c\u5217\u4e92\u6362\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\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<h2><strong>\u4f7f\u7528zip()\u51fd\u6570\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>transposed_matrix = [list(row) for row in zip(*matrix)]<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>zip(*matrix)<\/code>\u7684\u4f5c\u7528\u662f\u5c06\u77e9\u9635\u8fdb\u884c\u89e3\u5305\uff0c\u7136\u540e\u5c06\u76f8\u5e94\u4f4d\u7f6e\u7684\u5143\u7d20\u6253\u5305\u6210\u65b0\u7684\u5143\u7ec4\u3002\u518d\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u8fd9\u4e9b\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u6700\u7ec8\u5f97\u5230\u4e86\u7ffb\u8f6c\u540e\u7684\u4e8c\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u5217\u8868\u751f\u6210\u65b9\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u4e8c\u7ef4\u5217\u8868\u8fdb\u884c\u7ffb\u8f6c\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\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<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>transposed_matrix = [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u5916\u5c42\u5217\u8868\u63a8\u5bfc\u5f0f\u8fed\u4ee3\u5217\u7d22\u5f15<code>i<\/code>\uff0c\u5185\u5c42\u5217\u8868\u63a8\u5bfc\u5f0f\u8fed\u4ee3\u884c\u7d22\u5f15<code>j<\/code>\uff0c\u901a\u8fc7\u8bbf\u95ee<code>matrix[j][i]<\/code>\u6765\u91cd\u65b0\u6392\u5217\u5143\u7d20\uff0c\u6700\u7ec8\u5f62\u6210\u7ffb\u8f6c\u540e\u7684\u4e8c\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528numpy\u5e93\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868<\/h3>\n<\/p>\n<p><p>numpy\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u5177\u6709\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u4f7f\u7528numpy\u5e93\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5bf9\u4e8c\u7ef4\u5217\u8868\u8fdb\u884c\u7ffb\u8f6c\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/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<h2><strong>\u4f7f\u7528numpy\u5e93\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>np_matrix = np.array(matrix)<\/p>\n<p>transposed_matrix = np_matrix.T.tolist()<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5c06\u4e8c\u7ef4\u5217\u8868\u8f6c\u6362\u4e3anumpy\u6570\u7ec4\uff0c\u7136\u540e\u901a\u8fc7<code>np_matrix.T<\/code>\u5c5e\u6027\u8fdb\u884c\u8f6c\u7f6e\u64cd\u4f5c\uff0c\u6700\u540e\u5c06\u8f6c\u7f6e\u540e\u7684numpy\u6570\u7ec4\u8f6c\u6362\u56de\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u624b\u52a8\u8fed\u4ee3\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u624b\u52a8\u8fed\u4ee3\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u4e8c\u7ef4\u5217\u8868\u7684\u7ffb\u8f6c\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u8f83\u4e3a\u7e41\u7410\uff0c\u4f46\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u7ffb\u8f6c\u7684\u8fc7\u7a0b\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\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<h2><strong>\u624b\u52a8\u8fed\u4ee3\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>transposed_matrix = []<\/p>\n<p>for i in range(len(matrix[0])):<\/p>\n<p>    new_row = []<\/p>\n<p>    for j in range(len(matrix)):<\/p>\n<p>        new_row.append(matrix[j][i])<\/p>\n<p>    transposed_matrix.append(new_row)<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u5916\u5c42\u5faa\u73af\u8fed\u4ee3\u5217\u7d22\u5f15<code>i<\/code>\uff0c\u5185\u5c42\u5faa\u73af\u8fed\u4ee3\u884c\u7d22\u5f15<code>j<\/code>\uff0c\u901a\u8fc7\u8bbf\u95ee<code>matrix[j][i]<\/code>\u5c06\u5143\u7d20\u91cd\u65b0\u6392\u5217\u5230\u65b0\u7684\u5217\u8868\u4e2d\uff0c\u6700\u7ec8\u5f62\u6210\u7ffb\u8f6c\u540e\u7684\u4e8c\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f\u4e0e\u6027\u80fd\u5bf9\u6bd4<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e0d\u540c\u7684\u65b9\u6cd5\u6709\u5404\u81ea\u7684\u9002\u7528\u573a\u666f\u548c\u6027\u80fd\u7279\u70b9\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>zip()\u51fd\u6570\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u3001\u7b80\u6d01\u5730\u7ffb\u8f6c\u8f83\u5c0f\u7684\u4e8c\u7ef4\u5217\u8868\uff0c\u6027\u80fd\u8f83\u597d\u3002<\/li>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u5e0c\u671b\u901a\u8fc7\u76f4\u63a5\u64cd\u4f5c\u5217\u8868\u5b9e\u73b0\u7ffb\u8f6c\u7684\u60c5\u51b5\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u9ad8\uff0c\u6027\u80fd\u8f83\u597d\u3002<\/li>\n<li><strong>numpy\u5e93\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6216\u9700\u8981\u8fdb\u884c\u590d\u6742\u6570\u503c\u8ba1\u7b97\u7684\u573a\u666f\uff0c\u6027\u80fd\u4f18\u5f02\uff0c\u4f46\u9700\u8981\u5f15\u5165\u5916\u90e8\u5e93\u3002<\/li>\n<li><strong>\u624b\u52a8\u8fed\u4ee3\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u5b66\u4e60\u548c\u7406\u89e3\u4e8c\u7ef4\u5217\u8868\u7ffb\u8f6c\u8fc7\u7a0b\uff0c\u4e0d\u63a8\u8350\u7528\u4e8e\u5b9e\u9645\u5e94\u7528\uff0c\u6027\u80fd\u4e00\u822c\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u4e8c\u7ef4\u5217\u8868\u7684\u7ffb\u8f6c\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip()\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001numpy\u5e93\u4ee5\u53ca\u624b\u52a8\u8fed\u4ee3\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5bf9\u4e8e\u4e00\u822c\u7684\u5e94\u7528\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528zip()\u51fd\u6570\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4eec\u7b80\u6d01\u3001\u9ad8\u6548\u4e14\u6613\u4e8e\u7406\u89e3\u3002\u800c\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6216\u8fdb\u884c\u590d\u6742\u6570\u503c\u8ba1\u7b97\u65f6\uff0c\u63a8\u8350\u4f7f\u7528numpy\u5e93\u3002\u624b\u52a8\u8fed\u4ee3\u65b9\u6cd5\u9002\u7528\u4e8e\u5b66\u4e60\u548c\u7406\u89e3\u7ffb\u8f6c\u8fc7\u7a0b\uff0c\u4f46\u4e0d\u63a8\u8350\u7528\u4e8e\u5b9e\u9645\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e8c\u7ef4\u5217\u8868\u7684\u7ffb\u8f6c\uff1f<\/strong><br \/>\u8981\u7ffb\u8f6c\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8005\u5185\u7f6e\u7684<code>zip<\/code>\u51fd\u6570\u3002\u4f7f\u7528<code>zip<\/code>\u53ef\u4ee5\u5c06\u884c\u8f6c\u6362\u4e3a\u5217\uff0c\u4ece\u800c\u5b9e\u73b0\u7ffb\u8f6c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">original_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nflipped_list = [list(row) for row in zip(*original_list)]\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\u6613\u61c2\uff0c\u800c\u4e14\u6548\u7387\u8f83\u9ad8\u3002<\/p>\n<p><strong>\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868\u65f6\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868\u65f6\uff0c\u8981\u786e\u4fdd\u6240\u6709\u5b50\u5217\u8868\u7684\u957f\u5ea6\u76f8\u540c\u3002\u5982\u679c\u5b50\u5217\u8868\u957f\u5ea6\u4e0d\u4e00\u81f4\uff0c\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u3002\u53ef\u4ee5\u4f7f\u7528<code>itertools.zip_longest<\/code>\u6765\u5904\u7406\u4e0d\u540c\u957f\u5ea6\u7684\u5b50\u5217\u8868\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u7ffb\u8f6c\u4e8c\u7ef4\u5217\u8868\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>zip<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u7ffb\u8f6c\u4e8c\u7ef4\u6570\u7ec4\u3002NumPy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\noriginal_array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\nflipped_array = original_array.T\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u636e\u5904\u7406\u7684\u60c5\u51b5\uff0c\u80fd\u591f\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u628a\u4e8c\u7ef4\u5217\u8868\u7ffb\u8f6c\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip()\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528numpy\u5e93\u3002 \u5176\u4e2d\uff0c\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1069596,"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\/1069585"}],"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=1069585"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069585\/revisions"}],"predecessor-version":[{"id":1069599,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069585\/revisions\/1069599"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069596"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}