Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University."— Presentation transcript:

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

2 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 … 12 34 12 34 12 34 12 34

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!

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] 12 34 12 34 12 34 12 34

5 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] 12 34 12 34 12 34 12 34 How can we access the value in row3, column 3? Let's say to print the value.

6 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]

7 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]] 12 34 12 34 12 34 12 34 Look similar?

8 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]] 12 34 12 34 12 34 12 34 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])

9 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]

10 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.

11 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

12 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.

13 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)

14 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.

15 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 {}.

16 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


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

Similar presentations


Ads by Google