Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.

Slides:



Advertisements
Similar presentations
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,
ORDER OF OPERATIONS BIDMAS.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
October 17, 2005ICP: Chapter 5: Lists and Dictionaries 1 Introduction to Computer Programming Chapter 5: Lists and Dictionaries Michael Scherger Department.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
If statements while loop for loop
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Built-in Data Structures in Python An Introduction.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
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.
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.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
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.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Python Data Structures CMSC 201. Built in Types Today we will be talking about some other built in types in python! Tuples Sets Dictionaries.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
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.
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.
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
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.
Age stage expectations The calculation policy is organised according to age stage expectations as set out in the National Curriculum 2014, however it.
String and Lists Dr. José M. Reyes Álamo.
Pseudocode Key Revision Points.
Algorithmic complexity: Speed of algorithms
2-D Lists Taken from notes by Dr. Neil Moore
CHAPTER THREE Sequences.
String and Lists Dr. José M. Reyes Álamo.
Algorithmic complexity: Speed of algorithms
Topics Sequences Introduction to Lists List Slicing
Multidimensional Arrays
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Programming Control Structures with JavaScript Part 2
Loops and Arrays in JavaScript
Lets Play with arrays Singh Tripty
Algorithmic complexity: Speed of algorithms
Topics Sequences Introduction to Lists List Slicing
2-Dimensional Lists (Matrices) in Python
Introduction to Computer Science
2-D Lists Taken from notes by Dr. Neil Moore
Python List.
Tuple.
Introduction to Computer Science
Presentation transcript:

Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University

Raster Values? A raster data model represents a surface divided into a regular grid of cells (i.e. rows and columns) How can we store hundreds and thousands of values? raster0_0=1 raster0_1=2 raster0_2=1 raster0_3=2 raster1_0=3 …

Lists list=[0,1,2,3] l=["or","a","list","can","be","strings"] Lists Lists store multiple elements in a single variable. An element may be : 1) a value such as 0 or "a string"; 2) a variable such as x; or 3) another list!

Raster Values with Lists We can use a list to store each row in the raster. This eliminates a large number of variables, because we have a single variable that stores the values in all the columns for each row row0=[1,2,1,2] row1=[3,4,3,4] row2=[1,2,1,2] row3=[3,4,3,4]

Raster Values with Lists We can use a list to store each row in the raster. This eliminates a large number of variables, because we have a single variable that stores the values in all the columns for each row row0=[1,2,1,2] row1=[3,4,3,4] row2=[1,2,1,2] row3=[3,4,3,4] How can we access the value in row3, column 3? Let's say to print the value.

List Indices Elements in a list are accessed using their location in the list known as an index. Indices always start at 0. They are access using a square bracket such as list_name[index]. row3=[3, 4, 3, 4] print("row3[0]=",row3[0]) print("row3[1]=",row3[1]) print("row3[2]=",row3[2]) print("row3[3]=",row3[3]) row3[0]row3[2] row3[1]row3[3]

Raster Values using One Nested List Recall lists can store other lists We can use a nested list to all the raster values in a single variable! raster=[ [1,2,1,2], [3,4,3,4], [1,2,1,2], [3,4,3,4]] * Alternatively (and more compactly) raster=[[1,2,1,2],[3,4,3,4],[1,2,1,2][3,4,3,4]] Look similar?

Raster Values using One Nested List Recall lists can store other lists We can use a nested list to all the raster values in a single variable! raster=[ [1,2,1,2], [3,4,3,4], [1,2,1,2], [3,4,3,4]] Look similar? raster[3][3] raster[3][2] raster[3][1] raster[3][0] print("raster[3][0]=",raster[3][0]) print("raster[3][1]=",raster[3][1]) print("raster[3][2]=",raster[3][2]) print("raster[3][3]=",raster[3][3])

Appending to Lists list=[0,1,2,3] list.append(4) list [0,1,2,3,4] list.append(10) list [0,1,2,3,4,10] list.append(0) list [0,1,2,3,4,10,0]

For Loops for row in raster: print("row=",row) row= [1, 2, 1, 2] row= [3, 4, 3, 4] row= [1, 2, 1, 2] row= [3, 4, 3, 4] The Result For loops For loops iterate over each element in a list.

For Loops for row in raster: print("row=",row) row= [1, 2, 1, 2] row= [3, 4, 3, 4] row= [1, 2, 1, 2] row= [3, 4, 3, 4] The Result For loops For loops iterate over each element in a list. Each line in the loop must be indented

Nested For Loops for row in raster: for element in row: print("element=",element) element= 1 element= 2 element= 1 … element= 4 The Result Nested For loops For loops can be nested to iterate over nested elements. For example, a nested list.

Nested For Loops Using Indices for r in range(0,4): for c in range(0,4): print("raster[",r,"][",c,"]=",raster[r][c]) raster[ 0 ][ 0 ]= 1 raster[ 0 ][ 1 ]= 2 raster[ 0 ][ 2 ]= 1 … raster[ 3 ][ 3 ]= 4 The Result Range Range is a function that in essence creates an array of numbers (ranging from 0 to 3 in this case)

Length of List for r in range(0,len(raster)): for c in range(0,len(raster[0])): print("raster[",r,"][",c,"]=",raster[r][c]) raster[ 0 ][ 0 ]= 1 raster[ 0 ][ 1 ]= 2 raster[ 0 ][ 2 ]= 1 … raster[ 3 ][ 3 ]= 4 The Result len function The len function will return the length of a list. For nested lists we can use raster[0] or raster[r] to have the len function return the length of the nested list.

Dictionary mydict = { 'x': 41.1, 'y': -81.3, 'v': 10 } print mydict.keys() # Print all keys print mydict['x'] # Print value for this key Dictionaries Dictionaries store multiple elements in a single variable (similar to lists). Elements are stored as unordered key: value pairs in curly brackets {}.

Try It 1.Add all the values for the "raster" example and print out the result. Double check it is correct. 2.Use the work above to calculate the average value for the raster. 3.You can also modify values in a raster. Multiply all values in the raster by 2 and print out the new raster using print(raster). Double check the results are: [[2, 4, 2, 4], [6, 8, 6, 8], [2, 4, 2, 4], [6, 8, 6, 8]] 4.Create a dictionary using 'lat', 'lon', and 'value' keys with a location and value of your choice and print them out