Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.

Similar presentations


Presentation on theme: "CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore."— Presentation transcript:

1 CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore

2 Nested loops Notice that in program 3, you had essentially a loop inside another loop: the main function had a loop that played the whole game, and inside the loop it called the play_a_round function which had a loop in it, for playing a round. This is called a nested loop. Which of the two loops iterates more times? the inner loop Player 1 roll1, player 1 roll2, player1 roll3,… Then player 2 roll1, player2 roll2, player2 roll3, … Once the inner loop finishes, go to the next iteration of the outer loop. What if something should need to happen before/after each round? Put it inside the outer loop but not in the inner loop

3 Counting iterations Let’s use nested loops to print out a multiplication table. mult-table.py

4 Counting iterations How any times did we print a number? 10 times in the first iteration 10 times in the second iteration And so on… 10 x 10 = 100 times altogether When the inner loop’s sequence is the same each time: Total iterations = outer iterations x inner iterations If the inner loop doesn’t execute the same number of times each time, add up all the inner iterations to find the total for i in range(5): for j in range(i): # note, this is an “i“ not a 1 print(i,j) How many times does the print execute? 0 + 1 + 2 + 3 + 4 = 10 times

5 Two-dimensional lists Nested loops are particularly useful when we have nested lists That is, a list that contains other lists. That is also called a two-dimensional list Why would we use a 2D list? For storing a table of data Anything you would put in a spreadsheet Weather forecasts: row = city, column = days, data in cells = temperatures Grade book: row = student, column = assignment, data in cells = grade Or a game board For example, an 8x8 chess board Make a list of 8 rows Each row is a list of 8 squares Matrices Very useful for representing pixels in a graphics window Used all the time for “big data”

6 Row-major order In this class we’ll look at a 2-D list as “row-major” order. That means that the rows are the horizontal elements of the table, and they are numbered from 0 at the first row, increasing down to the bottom row And columns are the vertical components of the table. Each column is numbered from 0 at the left end, increasing as you move to the right. When you refer to a single element of a 2-D list, the row is given first, then the column “matrix[3][2]” is in the 4 th row and 3 rd column Typically you put the row on the outer loop and the column on the inner loop to process each element of the 2-D list

7 Creating 2D lists There are (at least) two ways to make a 2D list Hard code it table = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]] Row 0 is [ 1, 2, 3 ], etc. Or build it a row at a time - this method is MUCH more flexible!! First, create an empty list to hold the rows, table = [ ] Then create the row lists and append them to the outer list for rownum in range(5): # 5 rows row = [0 ] * 3 # 3 columns table.append(row) Why not just say this? table = [ [ 0, 0, 0 ] ] * 5 That makes all the rows aliases of each other! You really only have ONE row, not 5!!

8 Accessing 2D lists Suppose you have a list with 5 rows and 3 columns, called table. How do we access the element in the second row, first column? Take it a step at a time (for explanation, not necessary every time): How do you access the second row? row = table[1] What type is the second row? a ONE D list So now to get to the first column in the second row, element = row[0] Or, to put it together as element = table[1][0] Summary: to access an element in a 2D list, use list2D[row][column] This is a variable name just like x. It can be assigned to, printed, compared to another variable or constant, used in calculations, etc.

9 Traversing a 2D list To iterate over the contents of a 2D list, we need a nested loop Outer loop: for each row Inner loop: for each column in that row If we only need the elements, not the subscripts: for row in table: # row is a 1D list for element in row: process the element If we DO need subscripts, use ranges instead for rowno in range(len(table)): # len = number of rows for colon in range(len(table[rowno])): len of one row process element in position [rowno][colno]

10 Changing a 2D list element If you need to change an element, you need its subscripts! Suppose you just wanted to set all elements of an existing table to 0 for r in table: # r is a list that is one row for el in r: # el is one element in the row el = 0 Does NOT accomplish what you want to do! It just sets a temporary variable el to zero, over and over! What you need is for i in range(len(table)): for j in range(len(table[i])): table[i][j] = 0 This refers to the actual table element! And is what you want!

11 A large example: tic-tac-toe We’ll use a 2D list to represent a tic-tac-toe board. Each element in the list will be “X”, “O” or a space Use loops to print the board and to check whether someone won tictactoe.py


Download ppt "CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore."

Similar presentations


Ads by Google