{"id":1494,"date":"2023-01-04T20:00:00","date_gmt":"2023-01-04T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=1494"},"modified":"2023-10-15T07:07:46","modified_gmt":"2023-10-14T22:07:46","slug":"list-transpose","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/list-transpose\/","title":{"rendered":"[Python] How to Transpose List [convert rows to columns]"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to transpose list.<\/span><\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p><\/p>\n\n\n\n<ul>\n<li>[Appendix] How to display a two-dimensional array<\/li>\n\n\n\n<li>Transpose a two-dimensional array by &#8220;for loop&#8221;.<\/li>\n\n\n\n<li>Transpose a two-dimensional array by &#8220;numpy&#8221;<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How to display a two-dimensional array<\/h2>\n\n\n\n<p>In the beginning, I show <span class=\"st-mymarker-s\">how to display a two-dimensional array<\/span>.<\/p>\n\n\n\n<p>Displaying a two-dimensional array as a matrix makes it easier to visualize transposition.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If you try to display two-dimensional array by print( ) function , it is not displayed like a matrix.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>matrix = &#091;\n&#091;11, 12, 13, 14],\n&#091;21, 22, 23, 24],\n&#091;31, 32, 33, 34],\n]\n\nprint(matrix)\n# &#091;&#091;11, 12, 13, 14], &#091;21, 22, 23, 24], &#091;31, 32, 33, 34]]<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>This makes it difficult to imagine transposition.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>I show two ways to display a two-dimensional array as a matrix.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">How to display a two-dimensional array as a matrix by using &#8220;for loop&#8221;<\/h3>\n\n\n\n<p>I show how to display a two-dimensional array as a matrix <span class=\"st-mymarker-s\">by using &#8220;for loop&#8221;<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>matrix = &#091;\n&#091;11, 12, 13, 14],\n&#091;21, 22, 23, 24],\n&#091;31, 32, 33, 34],\n]\n\nfor i in range(len(matrix)):\n    print(matrix&#091;i])\n\"\"\"\n&#091;11, 12, 13, 14]\n&#091;21, 22, 23, 24]\n&#091;31, 32, 33, 34]\n\"\"\"<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">How to display a two-dimensional array as a matrix by using &#8220;Unpacking&#8221; and delimiter &#8220;\\n&#8221;<\/h3>\n\n\n\n<p>You can unpack the elements by appending &#8221; * &#8221; to list.<\/p>\n\n\n\n<p>Unpacking list elements allows them to be separated by a delimiter when they are displayed by using print( ) function.<\/p>\n\n\n\n<p>By specifying &#8220;\\n&#8221; as the delimiter, a two-dimensional array can be displayed as a matrix.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>matrix = &#091;\n&#091;11, 12, 13, 14],\n&#091;21, 22, 23, 24],\n&#091;31, 32, 33, 34],\n]\n\nprint(*matrix, sep='\\n')\n\"\"\"\n&#091;11, 12, 13, 14]\n&#091;21, 22, 23, 24]\n&#091;31, 32, 33, 34]\n\"\"\"<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Transpose a two-dimensional array by &#8220;for loop&#8221;<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to transpose a two-dimensional array by &#8220;for loop&#8221;<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>matrix = &#091;\n&#091;11, 12, 13, 14],\n&#091;21, 22, 23, 24],\n&#091;31, 32, 33, 34],\n]\n\nmatrix_t = &#091;]\n\nfor i in range(len(matrix&#091;0])) :\n    tmp = &#091;]\n    for v in matrix :\n        tmp.append(v&#091;i])\n    matrix_t.append(tmp)\n\nprint(*matrix_t, sep='\\n')\n\"\"\"\n&#091;11, 21, 31]\n&#091;12, 22, 32]\n&#091;13, 23, 33]\n&#091;14, 24, 34]\n\"\"\"<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&#8221; for v in matrix &#8221; extracts rows in matrix one by one.<\/p>\n\n\n\n<p>[ 11, 12, 13, 14 ], [ 21, 22, 23, 24 ], [ 31, 32, 33, 34 ] are taken out in order.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If i = 0, then v[0], so the 0th element of each row is added to &#8220;tmp&#8221; list by &#8220;append&#8221; method.<\/p>\n\n\n\n<p>That is, &#8220;tmp&#8221; list contain [ 11, 21, 31 ].<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Transpose a two-dimensional array by &#8220;numpy&#8221;<\/h2>\n\n\n\n<p>This is the easiest way.<\/p>\n\n\n\n<p>A numerical library called <span class=\"st-mymarker-s\">&#8220;numpy&#8221;<\/span> can be used to easily transpose two-dimensional arrays.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>After converting a list to a numpy type, a two-dimensional array is transposed by simply appending &#8220;.T&#8221; to it.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import numpy as np\n\nmatrix = &#091;\n&#091;11, 12, 13, 14],\n&#091;21, 22, 23, 24],\n&#091;31, 32, 33, 34],\n]\n\nprint(type(matrix))\n# &lt;class 'list'&gt;\n\nmatrix_np = np.array(matrix)\nprint(type(matrix_np))\n# &lt;class 'numpy.ndarray'&gt;\n\nmatrix_np_t = matrix_np.T\nprint(matrix_np_t)\n\"\"\"\n&#091;&#091;11 21 31]\n&#091;12 22 32]\n&#091;13 23 33]\n&#091;14 24 34]\n\"\"\"\n\nmatrix_np_t_list = matrix_np_t.tolist()\nprint(type(matrix_np_t_list))\n# &lt;class 'list'&gt;<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In the part &#8220;matrix_np_t_list = matrix_np_t.tolist()&#8221;, an array of numpy type is converted back to list type.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to transpose list. How to display a two-dimensional array In the beginning, I &#8230; <\/p>\n","protected":false},"author":1,"featured_media":2290,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,11],"tags":[16,19],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1494"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=1494"}],"version-history":[{"count":66,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1494\/revisions"}],"predecessor-version":[{"id":6246,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1494\/revisions\/6246"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/2290"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=1494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=1494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=1494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}