Mini-Lecture 16. Nested Lists

Similar documents
Lecture 14. Nested Lists and Dictionaries

Review 4. Lists and Sequences

Overview of List Syntax

Programming for Electrical and Computer Engineers. Pointers and Arrays

CS 1110 Final, December 8th, Question Points Score Total: 100

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

Two Dimensional Arrays

CS 1110 Final, December 8th, Question Points Score Total: 100

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

CMSC131. Seating Chart

Lecture 12. Lists (& Sequences)

Arrays array array length fixed array fixed length array fixed size array Array elements and subscripting

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Question 1. CSC 120H1 F Midterm Test Solutions Fall 2017

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Pointers and Arrays. adopted from KNK C Programming : A Modern Approach

Abstract Data Types Chapter 1

Teaching London Computing

CS 1110 Final, December 17th, Question Points Score Total: 100

Introduction to Object-Oriented Programming

Chapter 7: Arrays CS 121. April 9, Department of Computer Science College of Engineering Boise State University. Chapter 7: Arrays CS / 41

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Multiple Choice Questions Consider the following method and code segment.

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

CS 1110 Prelim 2 November 13th, 2014

Multi-Dimensional Arrays and Vectors. Class 27

Lists, Tuples and Dictionaries. HORT Lecture 10 Instructor: Kranthi Varala

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

Computer Programming: C++

CS Prelim 1 Review Fall 2017

Lecture 10: Lists and Sequences

2.Raspberry PI: Architecture & Hardware Specifications

Declaring a 2D Array

CS 1110: Introduction to Computing Using Python Lists and Sequences

Generating and Automatically Tuning OpenCL Code for Sparse Linear Algebra

This was the second midterm from I have added a few comments in bold type, like this.

Introduction. Data in a table or a matrix can be represented using a two-dimensional array. For example:

How to declare an array in C?

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Arrays in Java Multi-dimensional Arrays

Types, lists & functions

Multidimensional Arrays

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

Python in Economics and Finance

Example 1: Color-to-Grayscale Image Processing

More non-primitive types Lesson 06

Review: Declaring Arrays

Structure and Interpretation of Computer Programs

Package barcoder. October 26, 2018

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

A+ Computer Science MATRICES

CSCI1101& 2nd&Test&Solutions& April&17,&2015&! 1.!!The!following!statements!are!executed:!! x=[3.5, -9, 2.0, 6.7, 4, 2]

CS Prelim 1 Review Fall 2013

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:

Oracle BI 11g R1: Build Repositories Course OR102; 5 Days, Instructor-led

CMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

In this Lecture. More SQL Data Definition. Deleting Tables. Creating Tables. ALTERing Columns. Changing Tables. More SQL

PYTHON CONTENT NOTE: Almost every task is explained with an example

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Looping. Arizona State University 1

CS Prelim 2 Review Fall 2018

CMSC131. Arrays: The Concept

framessp2015.notebook February 09, 2015

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Arrays. Chapter 7 Part 3 Multi-Dimensional Arrays

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming

Session 14 March 31, 2018

CS Prelim 1 Review Fall 2016

Structure and Interpretation of Computer Programs

A Star Schema Has One To Many Relationship Between A Dimension And Fact Table

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

CS 1110 Prelim 2 April 22, 2014

CS11 Introduction to C++ Fall Lecture 2

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Part 5: Grid Data Structures (CS AB only)

Emil Sekerinski, McMaster University, Winter Term 16/17 COMP SCI 1MD3 Introduction to Programming

Abstract Data Type (ADT) & ARRAYS ALGORITHMS & DATA STRUCTURES I COMP 221

Python Mini Lessons last update: May 29, 2018

CSE11 Lecture 20 Fall 2013 Recursion

Array. Prepared By - Rifat Shahriyar

AP Computer Science A 2013 Free-Response Questions

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

This Oracle BI 11g R1: Build Repositories training is

Lecture 7. Scientific Computation

For Teacher's Use Only Q No Total Q No Q No

CS1110 Lab 6 (Mar 17-18, 2015)

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

CS 307 Midterm 1 Spring 2009

Lecture 25. Sequence Algorithms (Continued)

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

MIDTERM EXAMINATION - CS130 - Spring 2003

PREPARING FOR THE FINAL EXAM

Multidimensional Arrays. CSE 114, Computer Science 1 Stony Brook University

CS 230 Programming Languages

Transcription:

Mini-Lecture 16 Nested Lists

Nested Lists Lists can hold any object Lists are themselves objects Therefore lists can hold other lists! a = [2, 1] b = [3, 1] c = [1, 4, b] x = [1, a, c, 5] x[1] x[2] x[2][2] x = [1, [2, 1], [1, 4, [3, 1]], 5] x[0] x[1][1] x[2][0] x[2][2][1] 10/5/18 Nested Lists 2

Two Dimensional Lists Table of Data Images 0 1 2 3 0 1 2 3 4 5 6 7 8 9 10 1112 0 1 2 3 4 5 4 7 3 4 8 9 7 5 1 2 3 4 1 2 9 6 7 8 0 Each row, col has a value 0 1 2 3 4 5 6 7 8 9 10 11 12 Each row, col has an RGB value Store them as lists of lists (row-major order) d = [[5,4,7,3],[4,8,9,7],[5,1,2,3],[4,1,2,9],[6,7,8,0]] 10/5/18 Nested Lists 3

Overview of Two-Dimensional Lists Access value at row 3, col 2: 0 1 2 3 d[3][2] Assign value at row 3, col 2: d[3][2] = 8 d 0 1 2 3 5 4 7 3 4 8 9 7 5 1 2 3 4 1 2 9 An odd symmetry 4 6 7 8 0 Number of rows of d: len(d) Number of cols in row r of d: len(d[r]) 10/5/18 Nested Lists 4

How Multidimensional Lists are Stored b = [[9, 6, 4], [5, 7, 7]] b 9 6 4 5 7 7 9 6 4 5 7 7 b holds name of a one-dimensional list Has len(b) elements Its elements are (the names of) 1D lists b[i] holds the name of a one-dimensional list (of ints) Has len(b[i]) elements 10/5/18 Nested Lists 5

Ragged Lists: Rows w/ Different Length b = [[17,13,19],[28,95]] b 0 17 0 0 1 13 1 1 2 19 28 95 Will see applications of this later 10/5/18 Nested Lists 6

Slices and Multidimensional Lists Only top-level list is copied. Contents of the list are not altered b = [[9, 6], [4, 5], [7, 7]] x x = b[:2] id5 b id5 9 6 id4 id4 4 5 7 7 10/5/18 Nested Lists 7

Slices and Multidimensional Lists Only top-level list is copied. Contents of the list are not altered b = [[9, 6], [4, 5], [7, 7]] x x = b[:2] id5 b id5 9 6 id4 id4 4 5 7 7 10/5/18 Nested Lists 8

Slices and Multidimensional Lists Create a nested list >>> b = [[9,6],[4,5],[7,7]] Get a slice >>> x = b[:2] Append to a row of x >>> x[1].append(10) x now has nested list [[9, 6], [4, 5, 10]] What are the contents of the list (with name) in b? A: [[9,6],[4,5],[7,7]] B: [[9,6],[4,5,10]] C: [[9,6],[4,5,10],[7,7]] D: [[9,6],[4,10],[7,7]] E: I don t know 10/5/18 Nested Lists 9

Slices and Multidimensional Lists Create a nested list >>> b = [[9,6],[4,5],[7,7]] Get a slice >>> x = b[:2] Append to a row of x >>> x[1].append(10) x now has nested list [[9, 6], [4, 5, 10]] What are the contents of the list (with name) in b? A: [[9,6],[4,5],[7,7]] B: [[9,6],[4,5,10]] C: [[9,6],[4,5,10],[7,7]] D: [[9,6],[4,10],[7,7]] E: I don t know 10/5/18 Nested Lists 10

Functions and 2D Lists def transpose(table): """Returns: copy of table with rows and columns swapped Precondition: table is a (non-ragged) 2d List""" numrows = len(table) # Need number of rows numcols = len(table[0]) # All rows have same no. cols result = [] # Result (new table) accumulator for m in range(numcols): # Get the column elements at position m # Make a new list for this column # Add this row to accumulator table 1 2 3 4 5 6 1 3 5 2 4 6 return result 10/5/18 Nested Lists 11

Functions and 2D Lists def transpose(table): """Returns: copy of table with rows and columns swapped Precondition: table is a (non-ragged) 2d List""" numrows = len(table) # Need number of rows numcols = len(table[0]) # All rows have same no. cols result = [] for m in range(numcols): row = [] for n in range(numrows): # Result (new table) accumulator # Single row accumulator row.append(table[n][m]) # Create a new row list result.append(row) # Add result to table return result 1 2 3 4 5 6 1 3 5 2 4 6 10/5/18 Nested Lists 12

Functions and 2D Lists def transpose(table): """Returns: copy of table with rows and columns swapped Precondition: table is a (non-ragged) 2d List""" numrows = len(table) # Need number of rows numcols = len(table[0]) # All rows have same no. cols result = [] for m in range(numcols): row = [] for n in range(numrows): # Result (new table) accumulator # Single row accumulator row.append(table[n][m]) # Create a new row list result.append(row) # Add result to table return result Nest lists need nested loops 1 2 3 4 5 6 1 3 5 2 4 6 10/5/18 Nested Lists 13

JSON: Mixing and Lists and Dictionaries { } "wind" : { "speed" : 13.0, "crosswind" : 5.0 }, "sky" : [ { "cover" : "clouds", "type" : "broken", "height" : 1200.0 }, { "type" : "overcast", "height" : 1800.0 } ] Nested List Nested Dictionary Nested Dictionary weather.json: Weather measurements at Ithaca Airport (2017) Keys: Times (Each hour) Values: Weather readings This is a nested JSON Values are also dictionaries Containing more dictionaries And also containing lists See weather.py 10/5/18 Nested Lists 14