{"id":688,"date":"2021-06-24T13:11:24","date_gmt":"2021-06-24T12:11:24","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=688"},"modified":"2021-06-24T15:22:04","modified_gmt":"2021-06-24T14:22:04","slug":"2-d-arrays","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/adv-higher\/2-d-arrays\/","title":{"rendered":"2-D Arrays"},"content":{"rendered":"<p>A 2-D array is an &#8220;array or arrays&#8221;, with 2 index values:<\/p>\n<pre class=\"brush: python; highlight: [5]; title: Python; notranslate\" title=\"Python\">\r\nROOMS = 4\r\nCOMPUTERS = 5\r\n\r\n#create array\r\ncomputers = &#x5B; &#x5B;&quot;&quot; for c in range(COMPUTERS)] for r in range(ROOMS)]\r\n...\r\n<\/pre>\n<p>Nested loops are used to go through each of the index values:<\/p>\n<pre class=\"brush: python; highlight: [3,4,7]; title: Python; notranslate\" title=\"Python\">\r\n...\r\n#populate array\r\nfor room in range(ROOMS):\r\n    for computer in range(COMPUTERS):\r\n \r\n        computerID = f&quot;r{room}c{computer}&quot;\r\n        computers&#x5B;room]&#x5B;computer] = computerID\r\n\r\nprint(computers)\r\n<\/pre>\n<p>Printing a raw 2-D array shows the &#8220;array of arrays&#8221; structure:<\/p>\n<pre>[['r0c0', 'r0c1', 'r0c2', 'r0c3', 'r0c4'], ['r1c0', \n'r1c1', 'r1c2', 'r1c3', 'r1c4'], ['r2c0', 'r2c1', 'r2c2', \n'r2c3', 'r2c4'], ['r3c0', 'r3c1', 'r3c2', 'r3c3', 'r3c4']]\n<\/pre>\n<p>The printing of a 2-D array can be improved:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfor room in range(ROOMS):\r\n    for computer in range(COMPUTERS):\r\n        print(computers&#x5B;room]&#x5B;computer],' ',end='')\r\n    print()\r\n<\/pre>\n<pre>r0c0 r0c1 r0c2 r0c3 r0c4 \nr1c0 r1c1 r1c2 r1c3 r1c4 \nr2c0 r2c1 r2c2 r2c3 r2c4 \nr3c0 r3c1 r3c2 r3c3 r3c4\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A 2-D array is an &#8220;array or arrays&#8221;, with 2 index values: ROOMS = 4 COMPUTERS = 5 #create array computers = &#x5B; &#x5B;&quot;&quot; for c in range(COMPUTERS)] for r in range(ROOMS)] &#8230; Nested loops are used to go through each of the index values: &#8230; #populate array for room in range(ROOMS): for computer in range(COMPUTERS): computerID = f&quot;r{room}c{computer}&quot; computers&#x5B;room]&#x5B;computer] = computerID print(computers) Printing a raw 2-D array shows the<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/adv-higher\/2-d-arrays\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":291,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-688","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=688"}],"version-history":[{"count":3,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/688\/revisions"}],"predecessor-version":[{"id":693,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/688\/revisions\/693"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/291"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}