{"id":982147,"date":"2024-12-27T07:08:27","date_gmt":"2024-12-26T23:08:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/982147.html"},"modified":"2024-12-27T07:08:30","modified_gmt":"2024-12-26T23:08:30","slug":"python-%e7%9f%a9%e9%98%b5%e5%a6%82%e4%bd%95%e8%bd%ac%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/982147.html","title":{"rendered":"python \u77e9\u9635\u5982\u4f55\u8f6c\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210754\/5f4b4219-cdf4-4e19-b0cc-fe948a9a2858.webp\" alt=\"python \u77e9\u9635\u5982\u4f55\u8f6c\u7f6e\" \/><\/p>\n<p><p> \u8981\u8f6c\u7f6ePython\u4e2d\u7684\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528\u51e0\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d<strong>NumPy\u5e93\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00<\/strong>\uff0c\u5176\u4ed6\u65b9\u6cd5\u5305\u62ec<strong>\u5217\u8868\u89e3\u6790<\/strong>\u548c<strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u5b9e\u73b0\u77e9\u9635\u7684\u8f6c\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001NUMPY\u5e93<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002\u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u8f6c\u7f6e\u662f\u6700\u7b80\u5355\u548c\u6700\u5feb\u6377\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165NumPy\u5e93\u5e76\u521b\u5efa\u77e9\u9635<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165NumPy\u5e93\u5e76\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\u3002NumPy\u4e2d\u7684\u6570\u7ec4\u5bf9\u8c61\u79f0\u4e3andarray\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528numpy.array()\u51fd\u6570\u6765\u521b\u5efa\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2x3\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528.T\u5c5e\u6027\u8f6c\u7f6e\u77e9\u9635<\/li>\n<\/ol>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8f6c\u7f6e\u77e9\u9635\uff0c\u5373\u4f7f\u7528.T\u5c5e\u6027\u3002\u8fd9\u4e2a\u5c5e\u6027\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u8f6c\u7f6e\u77e9\u9635\uff0c\u800c\u4e0d\u4f1a\u4fee\u6539\u539f\u59cb\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u7f6e\u77e9\u9635<\/p>\n<p>transposed_matrix = matrix.T<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528numpy.transpose()\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u9664\u4e86.T\u5c5e\u6027\uff0cNumPy\u8fd8\u63d0\u4f9b\u4e86numpy.transpose()\u51fd\u6570\u6765\u8f6c\u7f6e\u77e9\u9635\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u591a\u7ef4\u6570\u7ec4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528numpy.transpose()\u51fd\u6570<\/p>\n<p>transposed_matrix = np.transpose(matrix)<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5217\u8868\u89e3\u6790<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528NumPy\u5e93\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u77e9\u9635\u8f6c\u7f6e\u3002\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u800c\u5f3a\u5927\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u77e9\u9635<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff0c\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a2x3\u7684\u77e9\u9635<\/p>\n<p>matrix = [[1, 2, 3],<\/p>\n<p>          [4, 5, 6]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u8f6c\u7f6e\u77e9\u9635<\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u521b\u5efa\u8f6c\u7f6e\u77e9\u9635\u3002\u901a\u8fc7\u8fed\u4ee3\u77e9\u9635\u7684\u5217\u7d22\u5f15\u5e76\u63d0\u53d6\u6bcf\u4e00\u5217\u7684\u5143\u7d20\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u89e3\u6790\u8f6c\u7f6e\u77e9\u9635<\/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>\u4e09\u3001\u81ea\u5b9a\u4e49\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u591a\u6b21\u4f7f\u7528\u8f6c\u7f6e\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u5b9e\u73b0\u77e9\u9635\u8f6c\u7f6e\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u77e9\u9635\u8f6c\u7f6e\uff0c\u800c\u65e0\u9700\u4f9d\u8d56\u5916\u90e8\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b9a\u4e49\u8f6c\u7f6e\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u63a5\u53d7\u4e00\u4e2a\u77e9\u9635\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u8f6c\u7f6e\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def transpose(matrix):<\/p>\n<p>    return [[row[i] for row in matrix] for i in range(len(matrix[0]))]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8f6c\u7f6e\u77e9\u9635<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8f6c\u7f6e\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a2x3\u7684\u77e9\u9635<\/p>\n<p>matrix = [[1, 2, 3],<\/p>\n<p>          [4, 5, 6]]<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8f6c\u7f6e\u77e9\u9635<\/strong><\/h2>\n<p>transposed_matrix = transpose(matrix)<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528Pandas\u5e93<\/p>\n<\/p>\n<p><p>Pandas\u662f\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u79d1\u5b66\u9886\u57df\u3002Pandas\u7684DataFrame\u5bf9\u8c61\u4e5f\u652f\u6301\u8f6c\u7f6e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165Pandas\u5e93\u5e76\u521b\u5efaDataFrame<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Pandas\u5e93\u5e76\u521b\u5efa\u4e00\u4e2aDataFrame\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528.T\u5c5e\u6027\u8f6c\u7f6eDataFrame<\/li>\n<\/ol>\n<p><p>\u4e0eNumPy\u7c7b\u4f3c\uff0cPandas\u7684DataFrame\u5bf9\u8c61\u4e5f\u6709\u4e00\u4e2a.T\u5c5e\u6027\u7528\u4e8e\u8f6c\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u7f6eDataFrame<\/p>\n<p>transposed_df = df.T<\/p>\n<p>print(transposed_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u8f6c\u7f6e\u77e9\u9635\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>NumPy\u5e93<\/strong>\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u77e9\u9635\u64cd\u4f5c\uff0c\u662f\u79d1\u5b66\u8ba1\u7b97\u7684\u9996\u9009\uff1b<strong>\u5217\u8868\u89e3\u6790<\/strong>\u9002\u5408\u5728\u4e0d\u4f7f\u7528\u5916\u90e8\u5e93\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u7b80\u5355\u8f6c\u7f6e\uff1b<strong>\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\u4fbf\u4e8e\u590d\u7528\u8f6c\u7f6e\u903b\u8f91\uff1b\u800c<strong>Pandas\u5e93<\/strong>\u5219\u5728\u6570\u636e\u5206\u6790\u4e2d\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u548c\u64cd\u4f5c\u529f\u80fd\u3002\u6839\u636e\u9700\u6c42\u7684\u4e0d\u540c\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6216NumPy\u5e93\u6765\u521b\u5efa\u77e9\u9635\u3002\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n<\/code><\/pre>\n<p>\u5982\u679c\u4f7f\u7528NumPy\u5e93\uff0c\u521b\u5efa\u77e9\u9635\u7684\u4ee3\u7801\u4e3a\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmatrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n<\/code><\/pre>\n<p>\u4f7f\u7528NumPy\u53ef\u4ee5\u7b80\u5316\u77e9\u9635\u7684\u64cd\u4f5c\u548c\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u8f6c\u7f6e\u77e9\u9635\u7684\u5e38\u89c1\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u77e9\u9635\u8f6c\u7f6e\u5728\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u6709\u5e7f\u6cdb\u5e94\u7528\u3002\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u5305\u62ec\uff1a  <\/p>\n<ol>\n<li>\u6570\u636e\u9884\u5904\u7406\uff1a\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u8f6c\u7f6e\uff0c\u4ee5\u4fbf\u5c06\u7279\u5f81\u548c\u6807\u7b7e\u5206\u5f00\u3002<\/li>\n<li>\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff1a\u5728\u6c42\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4\u65f6\uff0c\u8f6c\u7f6e\u77e9\u9635\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u6b65\u9aa4\u3002<\/li>\n<li>\u56fe\u50cf\u5904\u7406\uff1a\u56fe\u50cf\u6570\u636e\u901a\u5e38\u4ee5\u77e9\u9635\u5f62\u5f0f\u5b58\u50a8\uff0c\u8f6c\u7f6e\u64cd\u4f5c\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u7684\u65cb\u8f6c\u548c\u7ffb\u8f6c\u3002<\/li>\n<\/ol>\n<p><strong>\u4f7f\u7528NumPy\u5982\u4f55\u8fdb\u884c\u77e9\u9635\u8f6c\u7f6e\uff1f<\/strong><br \/>\u5728NumPy\u4e2d\uff0c\u8f6c\u7f6e\u77e9\u9635\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>.T<\/code>\u5c5e\u6027\u6765\u5b9e\u73b0\u8f6c\u7f6e\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmatrix = np.array([[1, 2, 3], [4, 5, 6]])\ntransposed_matrix = matrix.T\nprint(transposed_matrix)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u8f6c\u7f6e\u540e\u7684\u77e9\u9635\u3002NumPy\u7684\u8f6c\u7f6e\u64cd\u4f5c\u4e0d\u4ec5\u9ad8\u6548\uff0c\u800c\u4e14\u80fd\u591f\u5904\u7406\u591a\u7ef4\u6570\u7ec4\uff0c\u4f7f\u5f97\u6570\u636e\u5904\u7406\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8f6c\u7f6ePython\u4e2d\u7684\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528\u51e0\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2dNumPy\u5e93\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5176\u4ed6\u65b9\u6cd5\u5305\u62ec\u5217\u8868\u89e3\u6790\u548c\u81ea\u5b9a\u4e49 [&hellip;]","protected":false},"author":3,"featured_media":982157,"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\/982147"}],"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=982147"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982147\/revisions"}],"predecessor-version":[{"id":982159,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982147\/revisions\/982159"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/982157"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=982147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=982147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=982147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}