{"id":1036159,"date":"2024-12-31T12:04:41","date_gmt":"2024-12-31T04:04:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1036159.html"},"modified":"2024-12-31T12:04:44","modified_gmt":"2024-12-31T04:04:44","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%97%e8%a1%a8%e8%bd%ac%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1036159.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/8fa5685f-d1e4-4c20-b03e-3ccd95a17ca0.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u5217\u8868\u8f6c\u7f6e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u8f6c\u7f6e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f8b\u5982\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001zip\u51fd\u6570\u3001NumPy\u5e93\u7b49\u3002\u4ee5\u4e0b\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u8f6c\u7f6e\u3001\u4f7f\u7528zip\u51fd\u6570\u3001\u4f7f\u7528NumPy\u5e93<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528zip\u51fd\u6570<\/strong>\uff1a<\/p>\n<p>Python\u4e2d\u7684zip\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u8fed\u4ee3\u5668\u5408\u5e76\u4e3a\u4e00\u4e2a\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\uff0c\u901a\u8fc7\u7ed3\u5408*\u8fd0\u7b97\u7b26\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5217\u8868\u7684\u8f6c\u7f6e\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5217\u8868<\/p>\n<p>original_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u4f7f\u7528zip\u51fd\u6570\u8f6c\u7f6e\u5217\u8868<\/strong><\/h2>\n<p>transposed_list = [list(row) for row in zip(*original_list)]<\/p>\n<p>print(transposed_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528*original_list\u4f1a\u5c06\u539f\u59cb\u5217\u8868\u89e3\u5305\u6210\u591a\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9zip\u51fd\u6570\uff0czip\u4f1a\u5c06\u8fd9\u4e9b\u53c2\u6570\u7ec4\u5408\u6210\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\uff0c\u7136\u540e\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u6bcf\u4e2a\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u6700\u7ec8\u5b9e\u73b0\u5217\u8868\u7684\u8f6c\u7f6e\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a<\/p>\n<p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u521b\u5efa\u5217\u8868\u7684\u4e00\u79cd\u7b80\u6d01\u65b9\u6cd5\uff0c\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5217\u8868\u7684\u8f6c\u7f6e\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5217\u8868<\/p>\n<p>original_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8f6c\u7f6e\u5217\u8868<\/strong><\/h2>\n<p>transposed_list = [[original_list[j][i] for j in range(len(original_list))] for i in range(len(original_list[0]))]<\/p>\n<p>print(transposed_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u5c42\u5faa\u73afi\u904d\u5386\u539f\u59cb\u5217\u8868\u7684\u5217\u7d22\u5f15\uff0c\u5185\u5c42\u5faa\u73afj\u904d\u5386\u539f\u59cb\u5217\u8868\u7684\u884c\u7d22\u5f15\uff0c\u901a\u8fc7original_list[j][i]\u53ef\u4ee5\u83b7\u53d6\u8f6c\u7f6e\u540e\u5bf9\u5e94\u4f4d\u7f6e\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528NumPy\u5e93<\/strong>\uff1a<\/p>\n<p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u7684\u51fd\u6570\uff0c\u5176\u4e2dtranspose\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5217\u8868\u7684\u8f6c\u7f6e\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u539f\u59cb\u5217\u8868<\/strong><\/h2>\n<p>original_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>np_array = np.array(original_list)<\/p>\n<h2><strong>\u4f7f\u7528transpose\u51fd\u6570\u8f6c\u7f6e\u6570\u7ec4<\/strong><\/h2>\n<p>transposed_array = np_array.transpose()<\/p>\n<h2><strong>\u5c06\u8f6c\u7f6e\u540e\u7684\u6570\u7ec4\u8f6c\u6362\u56de\u5217\u8868<\/strong><\/h2>\n<p>transposed_list = transposed_array.tolist()<\/p>\n<p>print(transposed_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u5c06\u539f\u59cb\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528transpose\u51fd\u6570\u8fdb\u884c\u8f6c\u7f6e\uff0c\u6700\u540e\u5c06\u8f6c\u7f6e\u540e\u7684\u6570\u7ec4\u8f6c\u6362\u56de\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0zip\u51fd\u6570\u7684\u5b9e\u73b0<\/strong>\uff1a<\/p>\n<p>\u4f7f\u7528zip\u51fd\u6570\u8fdb\u884c\u5217\u8868\u8f6c\u7f6e\u7684\u65b9\u6cd5\u662f\u6700\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u4e4b\u4e00\u3002zip\u51fd\u6570\u53ef\u4ee5\u540c\u65f6\u904d\u5386\u591a\u4e2a\u8fed\u4ee3\u5668\uff0c\u5e76\u5c06\u5b83\u4eec\u7684\u5bf9\u5e94\u5143\u7d20\u7ec4\u5408\u6210\u5143\u7ec4\u3002\u5f53\u7ed3\u5408*\u8fd0\u7b97\u7b26\u65f6\uff0c\u5b83\u4f1a\u5c06\u539f\u59cb\u5217\u8868\u89e3\u5305\u6210\u591a\u4e2a\u53c2\u6570\uff0c\u4f20\u9012\u7ed9zip\u51fd\u6570\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5217\u8868\u7684\u884c\u548c\u5217\u8fdb\u884c\u4ea4\u6362\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5217\u8868\u7684\u8f6c\u7f6e\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5217\u8868<\/p>\n<p>original_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u4f7f\u7528zip\u51fd\u6570\u8f6c\u7f6e\u5217\u8868<\/strong><\/h2>\n<p>transposed_list = [list(row) for row in zip(*original_list)]<\/p>\n<p>print(transposed_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0czip(*original_list)\u4f1a\u5c06\u539f\u59cb\u5217\u8868\u7684\u6bcf\u4e00\u884c\u89e3\u5305\u6210\u591a\u4e2a\u53c2\u6570\uff0c\u4f20\u9012\u7ed9zip\u51fd\u6570\uff0czip\u51fd\u6570\u4f1a\u5c06\u8fd9\u4e9b\u53c2\u6570\u7ec4\u5408\u6210\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\uff0c\u6700\u540e\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u6bcf\u4e2a\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u5b9e\u73b0\u5217\u8868\u7684\u8f6c\u7f6e\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong>\uff1a<\/p>\n<p>\u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u8f6c\u7f6e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f8b\u5982\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001zip\u51fd\u6570\u548cNumPy\u5e93\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\uff0c\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002\u4f7f\u7528zip\u51fd\u6570\u662f\u6700\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u800cNumPy\u5e93\u5219\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u7684\u573a\u666f\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5217\u8868\u7684\u8f6c\u7f6e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u8f6c\u7f6e\u4e00\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8005\u5185\u7f6e\u7684<code>zip<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u5217\u8868\u7684\u8f6c\u7f6e\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff08\u5373\u5217\u8868\u7684\u5217\u8868\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528<code>zip(*original_list)<\/code>\u6765\u8f7b\u677e\u8f6c\u7f6e\u5b83\u3002\u4f8b\u5982\uff0c<code>transposed_list = list(zip(*original_list))<\/code>\uff0c\u8fd9\u4f1a\u5c06\u539f\u59cb\u7684\u884c\u548c\u5217\u4e92\u6362\u3002<\/p>\n<p><strong>\u8f6c\u7f6e\u5217\u8868\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8f6c\u7f6e\u5217\u8868\u65f6\uff0c\u786e\u4fdd\u539f\u59cb\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5b50\u5217\u8868\u5177\u6709\u76f8\u540c\u7684\u957f\u5ea6\u3002\u5426\u5219\uff0c\u8f6c\u7f6e\u540e\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e0d\u89c4\u5219\u7684\u7ed3\u6784\uff0c\u5bfc\u81f4\u6570\u636e\u5904\u7406\u65f6\u51fa\u73b0\u95ee\u9898\u3002\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u5b50\u5217\u8868\u7684\u957f\u5ea6\u662f\u5426\u4e00\u81f4\uff0c\u53ef\u4ee5\u5728\u8f6c\u7f6e\u4e4b\u524d\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n<p><strong>\u5728\u8f6c\u7f6e\u5217\u8868\u540e\u5982\u4f55\u5904\u7406\u7a7a\u503c\u6216\u4e0d\u89c4\u5219\u6570\u636e\uff1f<\/strong><br \/>\u5982\u679c\u8f6c\u7f6e\u540e\u7684\u5217\u8868\u4e2d\u5305\u542b\u7a7a\u503c\u6216\u4e0d\u89c4\u5219\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u6e05\u6d17\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u8f6c\u7f6e\u540e\u904d\u5386\u65b0\u5217\u8868\uff0c\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u6765\u8fc7\u6ee4\u6389\u4e0d\u9700\u8981\u7684\u7a7a\u503c\u3002\u5728Python\u4e2d\uff0c\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u65f6\uff0c\u7075\u6d3b\u8fd0\u7528<code>None<\/code>\u6216\u81ea\u5b9a\u4e49\u7684\u5360\u4f4d\u7b26\u80fd\u5e2e\u52a9\u4f60\u4fdd\u6301\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u5217\u8868\u8f6c\u7f6e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4f8b\u5982\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001zip\u51fd\u6570\u3001NumPy\u5e93\u7b49\u3002\u4ee5\u4e0b\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1036164,"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\/1036159"}],"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=1036159"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036159\/revisions"}],"predecessor-version":[{"id":1036166,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036159\/revisions\/1036166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1036164"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1036159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1036159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1036159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}