Data Structures So far, we have seen native data structures: Simple values: int, float, Boolean Sequences: Range, string, list Tuple, dictionary (chapter.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Python Control of Flow.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Data Structures More List Methods Our first encoding Matrix.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Topics: Sequence Sequences Index into a sequence [] notation Slicing and other operations.
I Power Int 2 Computing Software Development High Level Language Constructs.
Built-in Data Structures in Python An Introduction.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
I Power Higher Computing Software Development High Level Language Constructs.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Sequences CMSC 120: Visualizing Information 2/26/08.
 Recall that when you wanted to solve a system of equations, you used to use two different methods.  Substitution Method  Addition Method.
Announcements No Labs / Recitation this week On Friday we will talk about Project 3 Release late afternoon / evening tomorrow Cryptography.
Arrays. Arrays are objects that help us organize large amounts of information.
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.
13.3 Product of a Scalar and a Matrix.  In matrix algebra, a real number is often called a.  To multiply a matrix by a scalar, you multiply each entry.
Topic: Binary Encoding – Part 2
String and Lists Dr. José M. Reyes Álamo.
College Algebra Chapter 6 Matrices and Determinants and Applications
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
PH2150 Scientific Computing Skills
Linear Algebra Review.
Tuples and Lists.
Computer Programming Fundamentals
Containers and Lists CIS 40 – Introduction to Programming in Python
Python Comprehension and Generators
2. Java language basics (2)
Advanced Algorithms Analysis and Design
Basic operators - strings
Numeric Arrays Numeric Arrays Chapter 4.
Functions Defined on General Sets
Section 7.4 Matrix Algebra.
Introduction to Strings
Introduction to Strings
Introduction to Python
RECORD. RECORD Gaussian Elimination: derived system back-substitution.
FP Foundations, Scheme In Text: Chapter 14.
CSCI N207 Data Analysis Using Spreadsheet
Coding Concepts (Basics)
CSC1018F: Intermediate Python
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Data Types and Data Structures
Python Primer 1: Types and Operators
Multidimensional array
Introduction to Strings
(more) Python.
CS1110 Today: collections.
Python Basics with Jupyter Notebook
15-110: Principles of Computing
Introduction to Computer Science
Matlab Training Session 2: Matrix Operations and Relational Operators
Introduction to Strings
Python Review
Working with Arrays in MATLAB
Class code for pythonroom.com cchsp2cs
Introduction to Strings
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Introduction to Computer Science
Presentation transcript:

Data Structures So far, we have seen native data structures: Simple values: int, float, Boolean Sequences: Range, string, list Tuple, dictionary (chapter 11) There are many more useful data structures, not part of the Python language How can we get and use those data structures?

Encoded data structures

Matrix indexing

Matrix encoding

Does this work? We lose a bit of information in this encoding Which numbers correspond to which row We must explicitly keep track of rows through a row length variable B = [1, 0, 0, 0.5, 3, 4, -1, -3, 6] rowLength = 3 B[rowLength*i +j]

Let’s check i = 0 j = 0 B[3*0 + 0] B = [1, 0, 0, 0.5, 3, 4, -1, -3, 6] rowLength = 3 B[rowLength*i +j] i = 1 j = 1 B[3*1 + 1] i = 2 j = 1 B[3*1 + 2]

Q: which mapping? stored as list A = [0,1,2,5,4,3], indexed zero-up: A[1][1] = 4 def get_Elt_1(i, k, A): p = i*3 + k return A[p] def get_Elt_3(i, k, A): p = i*3 + k - 1 return A[p] def get_Elt_2(i, k, A): p = k*3 + i return A[p] A)get_Elt_1 B)get_Elt_2 C)get_Elt_3

Another way to encode a Matrix Lets take a look at our example matrix What about this? B= [[1, 0, 0], [0.5, 3, 4], [-1, -3, 6]]

Better matrix encoding Matrix Encode matrix as a list of lists, each row a list: A = [ [1, 2, 3], [4, 5, 6] ] Python encoded indexing is now : All elements of A[0][k] are the first row All elements of A[1][k] are the second row The row length is reflected in the encoding structure In general, element A[i][k] is what we want, but with zero indexing: = A[i-1][k-1]

Why is this important? We can now write code that more closely resembles mathematical notation i.e., we can use x and y to index into our matrix B = [[1, 0, 0], [0.5, 3, 4], [-1, -3, 6]] for x in range(3): for y in range(3): print (B[x][y])

How do we get simple matrices programmed? Recall: we can use the “*” to create a multi element sequence: 6 * [0] results in a sequence of 6 0’s -- [0, 0, 0, 0, 0, 0] 3 * [0, 0] results in a sequence of 6 0’s -- [0, 0, 0, 0, 0, 0] 10 * [0, 1, 2] results in what?

What is going on under the hood? Python uses some algebraic conventions 3 * [0, 0] is short for [0, 0] + [0, 0] + [0, 0] We know that “+” concatenates two sequences together

Another way to define lists The ‘*’ construct works for repeating the same thing: 3 * [1,2] yields [1,2,1,2,1,2] Leveraging the for loop: [ for in range( ) ] creates a list executing the for-loop: L = [ ] for k in range( ): L.append( ) Example: [ 0 for i in range(6)] ≡ [0]*6 and yields [0, 0,0,0,0,0] Example: [ k for k in range(3)] yields [0, 1, 2] What does this do: [2*[0] for i in range(3)]?

Defining simple matrices 4-by-4 all zero matrix: [4*[0] for k in range(4)] 5-by-5 identity matrix: M = [5*[0] for j in range(5)] for j in range(5): M[j][j] = 1

Adding two matrices M1 = [ [1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0] ] M2 = [ [2, 4, 6, 0], [1, 3, 5, 0], [0, -1, -2, 0] ] M3= [ 4*[0] for i in range(3) ] for x in range(3): for y in range(4): M3[x][y]= M1[x][y]+M2[x][y] M3[i][k] = M1[i][k] + M2[i][k]

Matrix – vector multiplication

Data structures We have constructed our first data structure! As the name implies, we have given structure to the data The data corresponds to the elements in the matrix The structure is a list of lists The structure allows us to utilize math-like notation