1 00:00:00,970 --> 00:00:01,803 - [Instructor] In this video, 2 00:00:01,803 --> 00:00:04,550 I'd like to just do a brief demonstration 3 00:00:04,550 --> 00:00:07,000 of two dimensional lists. 4 00:00:07,000 --> 00:00:08,210 So, in Python 5 00:00:08,210 --> 00:00:11,160 the way we represent those is with a list 6 00:00:11,160 --> 00:00:14,930 that contains as its elements other lists. 7 00:00:14,930 --> 00:00:16,050 Now, it turns out 8 00:00:16,050 --> 00:00:17,760 that most of the time you're working 9 00:00:17,760 --> 00:00:19,760 with two-dimensional data structures, 10 00:00:19,760 --> 00:00:23,010 or potentially higher dimensional data structures. 11 00:00:23,010 --> 00:00:26,190 You're typically going to work with data structures 12 00:00:26,190 --> 00:00:30,830 that are more efficient than lists are in Python. 13 00:00:30,830 --> 00:00:32,870 So, a couple of lessons from now 14 00:00:32,870 --> 00:00:34,010 we're going to take a look 15 00:00:34,010 --> 00:00:37,800 at a library for high performance computing, 16 00:00:37,800 --> 00:00:40,400 called NumPy, Numerical Python 17 00:00:40,400 --> 00:00:43,060 which provides an array data structure 18 00:00:43,060 --> 00:00:45,750 that can be used for one-dimensional arrays, 19 00:00:45,750 --> 00:00:46,810 two-dimensional arrays, 20 00:00:46,810 --> 00:00:49,330 and higher dimensional arrays as well. 21 00:00:49,330 --> 00:00:50,870 And we'll also take a look 22 00:00:50,870 --> 00:00:53,810 at a popular library used in data science 23 00:00:53,810 --> 00:00:56,870 and artificial intelligence called Pandas, 24 00:00:56,870 --> 00:00:58,860 which provides both one-dimensional 25 00:00:58,860 --> 00:01:01,360 and two-dimensional data structures. 26 00:01:01,360 --> 00:01:04,680 Both of those libraries are used more frequently 27 00:01:04,680 --> 00:01:08,080 than just plain old every day two-dimensional lists, 28 00:01:08,080 --> 00:01:11,320 for maintaining two-dimensional data. 29 00:01:11,320 --> 00:01:13,350 Now, let me go ahead and paste in 30 00:01:13,350 --> 00:01:14,600 a snippet of code here 31 00:01:14,600 --> 00:01:17,260 that will create a two-dimensional list. 32 00:01:17,260 --> 00:01:18,120 In Python, 33 00:01:18,120 --> 00:01:21,770 a two-dimensional list again is just a list of lists. 34 00:01:21,770 --> 00:01:25,910 So, what I have here is a nested list structure. 35 00:01:25,910 --> 00:01:29,620 The outermost square brackets define a list 36 00:01:29,620 --> 00:01:33,910 and that lists elements are a sub-list, 37 00:01:33,910 --> 00:01:35,540 a second sub-list 38 00:01:35,540 --> 00:01:37,100 and a third sub-list. 39 00:01:37,100 --> 00:01:39,040 So, each of those sub-list 40 00:01:39,040 --> 00:01:43,200 represents a row in a table of values 41 00:01:43,200 --> 00:01:44,870 and then within each row, 42 00:01:44,870 --> 00:01:47,200 we have in this case four columns. 43 00:01:47,200 --> 00:01:52,200 The first row contains the value 77, 68, 86 and 73. 44 00:01:52,600 --> 00:01:55,760 The second row contains the next four values listed here, 45 00:01:55,760 --> 00:01:57,570 and the third row contains 46 00:01:57,570 --> 00:01:59,840 the last four values listed here. 47 00:01:59,840 --> 00:02:02,551 Now, if I go ahead and create that, 48 00:02:02,551 --> 00:02:06,370 that's going to now create the list in memory 49 00:02:06,370 --> 00:02:09,340 and conceptually that list is going 50 00:02:09,340 --> 00:02:11,070 to look something like this. 51 00:02:11,070 --> 00:02:13,130 Now, this is just conceptual. 52 00:02:13,130 --> 00:02:14,690 The actual memory layout 53 00:02:14,690 --> 00:02:17,310 is going to be a list object, 54 00:02:17,310 --> 00:02:20,330 that contains references to other list objects. 55 00:02:20,330 --> 00:02:22,250 But, in row zero 56 00:02:22,250 --> 00:02:23,870 we have the first four values 57 00:02:23,870 --> 00:02:25,970 that were defined in the first sub-list. 58 00:02:25,970 --> 00:02:27,110 In row one, 59 00:02:27,110 --> 00:02:30,000 we have the values that were defined in the second sub-list, 60 00:02:30,000 --> 00:02:31,960 and in row two we have the values 61 00:02:31,960 --> 00:02:33,920 that were defined in the third sub-list. 62 00:02:33,920 --> 00:02:35,090 And by the way, 63 00:02:35,090 --> 00:02:38,780 there is nothing that says that anyone of those rows 64 00:02:38,780 --> 00:02:41,700 has to have the same number of columns. 65 00:02:41,700 --> 00:02:45,660 You can have what we call jagged or ragged arrays 66 00:02:45,660 --> 00:02:47,560 in some programming languages, 67 00:02:47,560 --> 00:02:50,660 where each row could in fact have a different 68 00:02:50,660 --> 00:02:53,090 number of elements as well. 69 00:02:53,090 --> 00:02:56,280 Now, when you want to identify the elements... 70 00:02:56,280 --> 00:02:58,330 Whoops, I wanted to go to the next slide there. 71 00:02:58,330 --> 00:03:01,020 When you want to identify the elements, 72 00:03:01,020 --> 00:03:02,170 when you have one set 73 00:03:02,170 --> 00:03:05,870 of square brackets following a variable name, 74 00:03:05,870 --> 00:03:09,190 that normally is for a one-dimensional list. 75 00:03:09,190 --> 00:03:11,690 But, if you have multi-dimensional lists, 76 00:03:11,690 --> 00:03:14,010 then you use two sets of square brackets 77 00:03:14,010 --> 00:03:15,750 where the first one is the row 78 00:03:15,750 --> 00:03:17,490 and the second one is the column. 79 00:03:17,490 --> 00:03:22,060 So, in row zero the first dimensions subscript, 80 00:03:22,060 --> 00:03:24,250 or index is always going to be zero 81 00:03:24,250 --> 00:03:25,470 and then the columns will be 82 00:03:25,470 --> 00:03:28,520 zero, one, two and three respectively. 83 00:03:28,520 --> 00:03:30,020 And this is the same as it is 84 00:03:30,020 --> 00:03:34,620 in just about every C based programming language out there. 85 00:03:34,620 --> 00:03:39,110 So, let's see how we can use a nested for loop 86 00:03:39,110 --> 00:03:41,900 to process a two-dimensional list 87 00:03:41,900 --> 00:03:44,390 and display its contents. 88 00:03:44,390 --> 00:03:48,830 So, let me copy and paste a loop structure in here. 89 00:03:48,830 --> 00:03:52,300 So, here we have a nested for loop 90 00:03:52,300 --> 00:03:54,780 and in the first outer for loop here, 91 00:03:54,780 --> 00:03:57,530 we've defined a target variable called Row, 92 00:03:57,530 --> 00:03:59,800 that says for each of the rows 93 00:03:59,800 --> 00:04:02,460 in the two-dimensional lists A. 94 00:04:02,460 --> 00:04:06,680 What it's really saying is for each element in the list A, 95 00:04:06,680 --> 00:04:10,890 those elements happen to be themselves other lists. 96 00:04:10,890 --> 00:04:14,030 But, it's really just for each element in the list A, 97 00:04:14,030 --> 00:04:15,730 I wanna do something. 98 00:04:15,730 --> 00:04:19,700 And because each of those elements is itself a list, 99 00:04:19,700 --> 00:04:23,280 we can then iterate through that row 100 00:04:23,280 --> 00:04:26,330 as we called it based on the variable up above. 101 00:04:26,330 --> 00:04:28,560 And for each item in that row, 102 00:04:28,560 --> 00:04:30,780 we would then like to output the item 103 00:04:30,780 --> 00:04:32,700 followed by a space. 104 00:04:32,700 --> 00:04:34,360 Now, when we complete a row 105 00:04:34,360 --> 00:04:37,020 by finishing off the nested loop, 106 00:04:37,020 --> 00:04:39,150 we then just do an empty print statement, 107 00:04:39,150 --> 00:04:40,750 which moves the Output Cursor 108 00:04:40,750 --> 00:04:42,550 to the beginning of the next line. 109 00:04:42,550 --> 00:04:46,460 And here we can get a two-dimensional representation 110 00:04:46,460 --> 00:04:49,040 of what that array looks like. 111 00:04:49,040 --> 00:04:49,873 And by the way, 112 00:04:49,873 --> 00:04:54,530 when we get to Pandas later on a couple of lessons from now, 113 00:04:54,530 --> 00:04:57,210 one of the really cool things about that library 114 00:04:57,210 --> 00:05:01,210 is that it's capable of automatically formatting data 115 00:05:01,210 --> 00:05:02,880 in two-dimensions like this, 116 00:05:02,880 --> 00:05:04,650 and not only that, 117 00:05:04,650 --> 00:05:06,687 you can give the columns names, 118 00:05:06,687 --> 00:05:08,620 you can give the rows names 119 00:05:08,620 --> 00:05:12,830 separate from just the numeric index numbers as well, 120 00:05:12,830 --> 00:05:14,440 and lots of other cool things 121 00:05:14,440 --> 00:05:17,220 that you'll be able to do with that library. 122 00:05:17,220 --> 00:05:20,700 Now, if you're wondering the order in which 123 00:05:20,700 --> 00:05:22,930 these items will be visited, 124 00:05:22,930 --> 00:05:26,330 we can use another loop to help us figure that out. 125 00:05:26,330 --> 00:05:29,450 So, you may recall previously we introduced 126 00:05:29,450 --> 00:05:32,780 the Enumerate built in function from Python, 127 00:05:32,780 --> 00:05:34,740 which when you give it a sequence 128 00:05:34,740 --> 00:05:36,120 it gives you back Tuples 129 00:05:36,120 --> 00:05:40,880 containing index numbers and values from that sequence. 130 00:05:40,880 --> 00:05:44,250 Well, A is a list of lists, 131 00:05:44,250 --> 00:05:46,070 so when we enumerate A, 132 00:05:46,070 --> 00:05:48,610 what we get are the index numbers 133 00:05:48,610 --> 00:05:50,930 that represent those lists 134 00:05:50,930 --> 00:05:54,520 and each item is a list itself. 135 00:05:54,520 --> 00:05:55,920 The row as far as 136 00:05:55,920 --> 00:06:00,750 our two-dimensional data structure is concerned here. 137 00:06:00,750 --> 00:06:04,870 So, for each Index I and corresponding row, 138 00:06:04,870 --> 00:06:08,130 we would like to go ahead and enumerate that row 139 00:06:08,130 --> 00:06:11,550 and for each Index J and the corresponding item, 140 00:06:11,550 --> 00:06:14,810 we will display A Sub I, Sub J, 141 00:06:14,810 --> 00:06:18,020 so that we can see what it's actual index values are, 142 00:06:18,020 --> 00:06:21,670 followed by an equal sign and the item and then a space. 143 00:06:21,670 --> 00:06:24,550 So, at the end of a given row, 144 00:06:24,550 --> 00:06:26,540 I'll put a new line character as well. 145 00:06:26,540 --> 00:06:28,350 So, this is the output 146 00:06:28,350 --> 00:06:30,660 and we can see that first we output 147 00:06:30,660 --> 00:06:32,630 all the elements in row zero, 148 00:06:32,630 --> 00:06:33,560 then row one, 149 00:06:33,560 --> 00:06:35,683 then row two as you would expect.