{"id":1059933,"date":"2024-12-31T15:29:32","date_gmt":"2024-12-31T07:29:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1059933.html"},"modified":"2024-12-31T15:29:35","modified_gmt":"2024-12-31T07:29:35","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e5%88%97%e8%a1%a8%e5%86%85%e5%ae%b9%e8%bd%ac%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1059933.html","title":{"rendered":"\u5982\u4f55\u5c06python\u5217\u8868\u5185\u5bb9\u8f6c\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/1e9abf5c-e5e4-467e-bf03-253ffc0ca087.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5c06python\u5217\u8868\u5185\u5bb9\u8f6c\u7f6e\" \/><\/p>\n<p><p> <strong>\u5c06Python\u5217\u8868\u5185\u5bb9\u8f6c\u7f6e\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528NumPy\u5e93\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u6765\u51b3\u5b9a\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e00\u4e0b\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5185\u7f6e\u7684<code>zip<\/code>\u51fd\u6570\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<code>zip<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u8fed\u4ee3\u5668\u5408\u5e76\u6210\u4e00\u4e2a\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\uff0c\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>map<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>transposed_matrix = list(zip(*matrix))<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>*matrix<\/code>\u5c06\u539f\u59cb\u77e9\u9635\u89e3\u5305\u6210\u5355\u72ec\u7684\u5217\u8868\uff0c\u7136\u540e<code>zip<\/code>\u51fd\u6570\u5c06\u5bf9\u5e94\u7684\u5143\u7d20\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>zip<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u9ad8\u6548\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\uff09\u9010\u4e00\u914d\u5bf9\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u8fed\u4ee3\u5668\u3002\u5728\u5217\u8868\u8f6c\u7f6e\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u89e3\u5305\u64cd\u4f5c\uff08\u5373\u5728\u5217\u8868\u540d\u524d\u52a0\u4e0a\u661f\u53f7<code>*<\/code>\uff09\uff0c\u5c06\u4e00\u4e2a\u77e9\u9635\uff08\u5373\u4e00\u4e2a\u5217\u8868\u7684\u5217\u8868\uff09\u89e3\u5305\u6210\u591a\u4e2a\u72ec\u7acb\u7684\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>zip<\/code>\u51fd\u6570\u3002\u8fd9\u6837\uff0c<code>zip<\/code>\u51fd\u6570\u4f1a\u628a\u8fd9\u4e9b\u72ec\u7acb\u7684\u5217\u8868\u4e2d\u7684\u5bf9\u5e94\u5143\u7d20\u914d\u5bf9\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u5143\u7ec4\uff0c\u8fbe\u5230\u8f6c\u7f6e\u7684\u6548\u679c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\u4e14\u9ad8\u6548\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14Pythonic\u7684\u65b9\u5f0f\u6765\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u7684\u5217\u8868\u751f\u6210\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u77e9\u9635\u8f6c\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>transposed_matrix = [[row[i] for row in 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\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5916\u5c42\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u6bcf\u4e00\u5217\u7684\u7d22\u5f15\uff0c\u5185\u5c42\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u6bcf\u4e00\u884c\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u76f8\u5e94\u7684\u5143\u7d20\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u6700\u7ec8\u751f\u6210\u8f6c\u7f6e\u540e\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u5f3a\u5927\u4e4b\u5904\u5728\u4e8e\u5b83\u7684\u7b80\u6d01\u548c\u7075\u6d3b\u6027\u3002\u901a\u8fc7\u5d4c\u5957\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u904d\u5386\u539f\u59cb\u77e9\u9635\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u5916\u5c42\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u6bcf\u4e00\u5217\u7684\u7d22\u5f15\uff0c\u800c\u5185\u5c42\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u5219\u904d\u5386\u6bcf\u4e00\u884c\uff0c\u5e76\u63d0\u53d6\u5bf9\u5e94\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4ee3\u7801\u91cf\u7a0d\u591a\uff0c\u4f46\u80dc\u5728\u76f4\u89c2\uff0c\u53ef\u4ee5\u6e05\u6670\u5730\u5c55\u793a\u8f6c\u7f6e\u7684\u8fc7\u7a0b\uff0c\u975e\u5e38\u9002\u5408\u6559\u5b66\u6216\u7406\u89e3\u8f6c\u7f6e\u7684\u539f\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u6216\u9700\u8981\u9ad8\u6548\u8ba1\u7b97\u7684\u573a\u666f\uff0c\u63a8\u8350\u4f7f\u7528NumPy\u5e93\u3002NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u5176\u8f6c\u7f6e\u64cd\u4f5c\u4e5f\u975e\u5e38\u7b80\u5355\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import numpy as np<\/p>\n<p>matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>transposed_matrix = matrix.T<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u539f\u59cb\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u7136\u540e\u901a\u8fc7<code>.T<\/code>\u5c5e\u6027\u76f4\u63a5\u83b7\u53d6\u8f6c\u7f6e\u540e\u7684\u77e9\u9635\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6027\u80fd\u4f18\u8d8a\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u5e93\u662f\u5904\u7406\u6570\u503c\u6570\u636e\u7684\u5229\u5668\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u77e9\u9635\u548c\u591a\u7ef4\u6570\u7ec4\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528NumPy\u7684\u9ad8\u6548\u8ba1\u7b97\u80fd\u529b\uff0c\u5feb\u901f\u8fdb\u884c\u77e9\u9635\u7684\u8f6c\u7f6e\u64cd\u4f5c\u3002NumPy\u7684<code>.T<\/code>\u5c5e\u6027\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u83b7\u53d6\u8f6c\u7f6e\u77e9\u9635\u7684\u5feb\u6377\u65b9\u5f0f\uff0c\u5185\u90e8\u5df2\u7ecf\u4f18\u5316\u8fc7\uff0c\u6027\u80fd\u975e\u5e38\u4f18\u8d8a\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u7684\u77e9\u9635\u6570\u636e\u3002\u6b64\u5916\uff0cNumPy\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u51fd\u6570\uff0c\u5982\u77e9\u9635\u4e58\u6cd5\u3001\u9006\u77e9\u9635\u8ba1\u7b97\u7b49\uff0c\u975e\u5e38\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u9886\u57df\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6027\u80fd\u5bf9\u6bd4\u4e0e\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u8f6c\u7f6e\u65b9\u6cd5\u65f6\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u6027\u80fd\u3001\u4ee3\u7801\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u7b49\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e09\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\u5bf9\u6bd4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5185\u7f6e\u51fd\u6570zip<\/strong>\uff1a\u9002\u5408\u5c0f\u89c4\u6a21\u6570\u636e\uff0c\u7b80\u6d01\u4e14\u6613\u8bfb\u3002<\/li>\n<li><strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\uff1a\u9002\u5408\u4e2d\u7b49\u89c4\u6a21\u6570\u636e\uff0c\u4ee3\u7801\u76f4\u89c2\uff0c\u9002\u5408\u7406\u89e3\u8f6c\u7f6e\u539f\u7406\u3002<\/li>\n<li><strong>NumPy\u5e93<\/strong>\uff1a\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u548c\u590d\u6742\u8ba1\u7b97\uff0c\u6027\u80fd\u4f18\u8d8a\uff0c\u4ee3\u7801\u7b80\u6d01\u3002<\/li>\n<\/ol>\n<p><p>\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u53ea\u662f\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\uff0c\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u90fd\u8db3\u591f\u4e86\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c\u6216\u8005\u8fdb\u884c\u590d\u6742\u7684\u77e9\u9635\u8fd0\u7b97\uff0c\u63a8\u8350\u4f7f\u7528NumPy\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6848\u4f8b<\/h3>\n<\/p>\n<p><h4>\u6570\u636e\u5206\u6790\u4e2d\u7684\u77e9\u9635\u8f6c\u7f6e<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u8f6c\u7f6e\u64cd\u4f5c\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u3002\u4f8b\u5982\uff0c\u5f53\u6211\u4eec\u4ece\u6570\u636e\u5e93\u6216CSV\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u6570\u636e\u901a\u5e38\u4ee5\u884c\u8bb0\u5f55\u7684\u5f62\u5f0f\u5b58\u50a8\uff0c\u800c\u6211\u4eec\u9700\u8981\u4ee5\u5217\u8bb0\u5f55\u7684\u5f62\u5f0f\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u4eceCSV\u6587\u4ef6\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p>matrix = data.values<\/p>\n<h2><strong>\u8f6c\u7f6e\u77e9\u9635<\/strong><\/h2>\n<p>transposed_matrix = np.transpose(matrix)<\/p>\n<h2><strong>\u8f6c\u6362\u56deDataFrame<\/strong><\/h2>\n<p>transposed_data = pd.DataFrame(transposed_matrix)<\/p>\n<p>print(transposed_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4eceCSV\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>np.transpose<\/code>\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u8f6c\u7f6e\uff0c\u5e76\u5c06\u8f6c\u7f6e\u540e\u7684\u6570\u636e\u8f6c\u6362\u56dePandas DataFrame\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u77e9\u9635\u8f6c\u7f6e<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u901a\u5e38\u8868\u793a\u4e3a\u591a\u7ef4\u6570\u7ec4\uff08\u77e9\u9635\uff09\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5bf9\u56fe\u50cf\u8fdb\u884c\u8f6c\u7f6e\u64cd\u4f5c\uff0c\u4f8b\u5982\u65cb\u8f6c\u56fe\u50cf\u3001\u4ea4\u6362\u56fe\u50cf\u7684\u5bbd\u9ad8\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import numpy as np<\/p>\n<p>from PIL import Image<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf<\/strong><\/h2>\n<p>image = Image.open(&#39;image.jpg&#39;)<\/p>\n<p>image_array = np.array(image)<\/p>\n<h2><strong>\u8f6c\u7f6e\u56fe\u50cf<\/strong><\/h2>\n<p>transposed_image_array = np.transpose(image_array, (1, 0, 2))<\/p>\n<h2><strong>\u8f6c\u6362\u56de\u56fe\u50cf<\/strong><\/h2>\n<p>transposed_image = Image.fromarray(transposed_image_array)<\/p>\n<p>transposed_image.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528PIL\u5e93\u8bfb\u53d6\u56fe\u50cf\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>np.transpose<\/code>\u51fd\u6570\u5bf9\u56fe\u50cf\u6570\u7ec4\u8fdb\u884c\u8f6c\u7f6e\uff0c\u5e76\u6307\u5b9a\u8f74\u7684\u987a\u5e8f\uff0c\u4ee5\u5b9e\u73b0\u56fe\u50cf\u7684\u65cb\u8f6c\u3002\u6700\u540e\uff0c\u6211\u4eec\u5c06\u8f6c\u7f6e\u540e\u7684\u6570\u7ec4\u8f6c\u6362\u56de\u56fe\u50cf\u683c\u5f0f\uff0c\u5e76\u663e\u793a\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06Python\u5217\u8868\u5185\u5bb9\u8f6c\u7f6e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u6765\u51b3\u5b9a\u3002\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>zip<\/code>\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u548c\u4e2d\u7b49\u89c4\u6a21\u7684\u6570\u636e\uff0c\u800c\u4f7f\u7528NumPy\u5e93\u5219\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u548c\u590d\u6742\u8ba1\u7b97\u3002\u901a\u8fc7\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6848\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u77e9\u9635\u8f6c\u7f6e\u5728\u6570\u636e\u5206\u6790\u548c\u56fe\u50cf\u5904\u7406\u7b49\u9886\u57df\u4e2d\u7684\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u5217\u8868\u5185\u5bb9\u8f6c\u7f6e\u7684\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8f6c\u7f6e\u4e00\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8f6c\u7f6e\u4e00\u4e2a\u5217\u8868\u901a\u5e38\u610f\u5473\u7740\u5c06\u5176\u884c\u548c\u5217\u8fdb\u884c\u4e92\u6362\u3002\u5bf9\u4e8e\u4e8c\u7ef4\u5217\u8868\uff08\u5217\u8868\u7684\u5217\u8868\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>zip<\/code>\u51fd\u6570\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e8c\u7ef4\u5217\u8868\u7684\u884c\u548c\u5217\u8fdb\u884c\u8f6c\u7f6e\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">original_list = [[1, 2, 3], [4, 5, 6]]\ntransposed_list = [list(row) for row in zip(*original_list)]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>transposed_list<\/code>\u7684\u5185\u5bb9\u5c06\u53d8\u4e3a<code>[[1, 4], [2, 5], [3, 6]]<\/code>\u3002<\/p>\n<p><strong>\u4f7f\u7528NumPy\u5e93\u8f6c\u7f6e\u5217\u8868\u6709\u54ea\u4e9b\u4f18\u52bf\uff1f<\/strong><br \/>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u8f7b\u677e\u8f6c\u7f6e\u5217\u8868\uff0c\u5e76\u4e14\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u6027\u80fd\u66f4\u4f18\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\noriginal_array = np.array([[1, 2, 3], [4, 5, 6]])\ntransposed_array = original_array.T\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u5f97\u5230\u8f6c\u7f6e\u540e\u7684\u6570\u7ec4\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<p><strong>\u8f6c\u7f6e\u5217\u8868\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u8f6c\u7f6e\u5217\u8868\u65f6\uff0c\u786e\u4fdd\u539f\u59cb\u5217\u8868\u7684\u6bcf\u4e00\u884c\u5177\u6709\u76f8\u540c\u7684\u957f\u5ea6\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5982\u679c\u5217\u8868\u7684\u884c\u957f\u5ea6\u4e0d\u4e00\u81f4\uff0c\u8f6c\u7f6e\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7ed3\u679c\u6216\u9519\u8bef\u3002\u6b64\u5916\uff0c\u4e86\u89e3\u8f6c\u7f6e\u540e\u7684\u6570\u636e\u7ed3\u6784\u7c7b\u578b\u4e5f\u5f88\u91cd\u8981\uff0c\u786e\u4fdd\u5728\u540e\u7eed\u5904\u7406\u65f6\u9002\u914d\u76f8\u5e94\u7684\u6570\u636e\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Python\u5217\u8868\u5185\u5bb9\u8f6c\u7f6e\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570zip\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528NumPy\u5e93\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18 [&hellip;]","protected":false},"author":3,"featured_media":1059945,"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\/1059933"}],"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=1059933"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059933\/revisions"}],"predecessor-version":[{"id":1059946,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059933\/revisions\/1059946"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1059945"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1059933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1059933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1059933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}