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

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Advertisements

Introduction to Computing Science and Programming I
Conditionals Lab Dan Maselko. Overview  Gain familiarity with working with conditionals  Become familiar with using the modulus operator  Understand.
Arrays. INTRODUCTION TO ARRAYS Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found.
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
18-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Nested Loops. Nesting Control Structures One if statement inside another one An if statement inside a loop A loop inside an if statement Control structures.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Lists in Python.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
CSC Intro. to Computing Lecture 13: PALGO. Announcements Midterm is in one week  Time to start reviewing  We will do more review in class Tuesday.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Array Creation ENGR 1181 MATLAB 2. Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Control flow Ruth Anderson UW CSE 160 Spring
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
2-dimensional Arrays A 2-dimensional array has rows and columns It is actually an “array of arrays” A Tic-Tac-Toe board is an example of a 3 by 3 2-d array.
Control flow Ruth Anderson UW CSE 160 Winter
CS 221 – May 22 Timing (sections 2.6 and 3.6) Speedup Amdahl’s law – What happens if you can’t parallelize everything Complexity Commands to put in your.
Systems of Equations and Matrices Review of Matrix Properties Mitchell.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
12-4: Matrix Methods for Square Systems
13.4 Product of Two Matrices
1.5 Matricies.
Loops BIS1523 – Lecture 10.
Matrix 2015/11/18 Hongfei Yan zip(*a) is matrix transposition
CS 115 Lecture 8 Structured Programming; for loops
Matrix 2016/11/30 Hongfei Yan zip(*a) is matrix transposition
2-D Lists Taken from notes by Dr. Neil Moore
Topics Introduction to Repetition Structures
Intro to PHP & Variables
While Loops BIS1523 – Lecture 12.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Ruth Anderson UW CSE 160 Spring 2018
Data Structures – 1D Lists
Ruth Anderson UW CSE 140 Winter 2014
Topics Sequences Introduction to Lists List Slicing
Loop Statements & Vectorizing Code
Recursion Taken from notes by Dr. Neil Moore
Building Java Programs
For loops Taken from notes by Dr. Neil Moore
Topics Introduction to Repetition Structures
Building Java Programs
Topics Sequences Introduction to Lists List Slicing
Building Java Programs
Loop Statements & Vectorizing Code
2-D Lists Taken from notes by Dr. Neil Moore
Control flow: Loops UW CSE 160.
Presentation transcript:

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

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

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

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? = 10 times

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”

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

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!!

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.

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]

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!

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