Introduction to Computational Thinking Abstraction Data Structures more
Introduction to Computational Thinking Abstraction & Data Structures …more complex data structures (C) Dennis Kafura 2016 1
CT@VT Combining lists and dictionaries n Lists and dictionaries are two forms of n n data structures – different schemes to organize and access data Representing the abstractions of complex real-world artifacts often requires some combination of these data structures Combining simple structures to form more complex ones (C) Dennis Kafura 2016 Slide 2
CT@VT List of dictionaries n A weather report abstraction has three properties q q q n temperature humidity wind A weather forecast is a collection of instance of weather reports (C) Dennis Kafura 2016 Slide 3
CT@VT A collection of dictionaries n n Each instance of a weather report in the weather forecast can be represented separately Each instance of a weather report can be represented as a dictionary { “temperature”: 30, “humidity”: 20, “wind”: 3 } (C) Dennis Kafura 2016 Slide 4
A list of dictionaries CT@VT n The individual weather report instances (dictionaries) can be collectively represented in a list { ‘temperature’: 24, ‘humidity’: 30, ‘wind’: 19 } { ‘temperature’: 18, ‘humidity’: 90, ‘wind’: 15 } { ‘temperature’: 29, ‘humidity’: 100, ‘wind’: 5 } { ‘temperature’: 25, ‘humidity’: 50, ‘wind’: 10 } { ‘temperature’: 30, ‘humidity’: 20, ‘wind’: 3 } (C) Dennis Kafura 2016 Slide 5
CT@VT List/dictionary in Python { ‘temperature’: 24, ‘humidity’: 30, ‘wind’: 19 } { ‘temperature’: 18, ‘humidity’: 90, ‘wind’: 15 } { ‘temperature’: 29, ‘humidity’: 100, ‘wind’: 5 } { ‘temperature’: 25, ‘humidity’: 50, ‘wind’: 10 } { ‘temperature’: 30, ‘humidity’: 20, ‘wind’: 3 } [ {‘temperature’ {‘temperature’ : : : 30, 25, 29, 18, 24, ‘humidity’ ‘humidity’ : 20, : 50, : 100, : 90, : 30, (C) Dennis Kafura 2016 ‘wind’ ‘wind’ : 3} , : 10} , : 5} , : 19} ], Slide 6
CT@VT Next steps n Remember: q With a list iterate through each element n q With a dictionary use a key to access a value n n for <item> in <list>: value = dictionary[“key”] Now q q Do the first two problems Report/discuss (C) Dennis Kafura 2016 Slide 7
CT@VT A dictionary with list values n An abstraction of a temperature forecast for multiple cities City Forecast “Blacksburg, VA” [76, 54, 72, 52, 64, 43, 66, 48, 71, 54] “Seattle, WA” [63, 56, 69, 55, 69, 54, 67, 52, 63, 51] “Miami, FL” [86, 69, 87, 71, 88, 64, 88, 72, 87, 73] “San Jose, CA” [79, 57, 82, 57, 83, 58, 84, 60, 84] “New York, NY” [5, 8, 8, 3, 2, 1, 9, 5, -4, -10] (C) Dennis Kafura 2016 Slide 8
CT@VT “Nested” iteration n Nested: one (inner) iteration in the body of another (outer) iteration City for …. . : # each city for …. . : # each temperature Forecast “Blacksburg, VA” [ ] “Seattle, WA” [ ] “Miami, FL” [ ] “San Jose, CA” [ ] “New York, NY” [ ] (C) Dennis Kafura 2016 Slide 9
CT@VT Another graphic form [ list ] City Forecast “Blacksburg, VA” [76, 54, 72, 52, 64, 43, 66, 48, 71, 54] “Seattle, WA” [63, 56, 69, 55, 69, 54, 67, 52, 63, 51] “Miami, FL” [86, 69, 87, 71, 88, 64, 88, 72, 87, 73] “San Jose, CA” [79, 57, 82, 57, 83, 58, 84, 60, 84] “New York, NY” [5, 8, 8, 3, 2, 1, 9, 5, -4, -10] { City , Forecast } string [ list ] int(eger) (C) Dennis Kafura 2016 Slide 10
CT@VT Classwork n n Do problem 3 Report/discussion (C) Dennis Kafura 2016 Slide 11
CT@VT Layers of Abstraction n Many properties are often needed make our abstractions of the real world accurate Humans are not “wired” to handle many properties at one time (our short term memory allows about 7 +/- 1 things) We have devised ways of coping with this complexity through layers or hierarchy q q q An entity is represented by few parts Each part is represented by a few sub-parts Each sub-part is …. (C) Dennis Kafura 2016 Slide 12
CT@VT Layers of Abstraction Place Report Date . . Temperature Humidity Wind 76 55 18 City State Zip Month Day Year Blacksburg VA 24061 May 20 2015 (C) Dennis Kafura 2016 Hour Min Sec 11 32 52 Time Slide 13
CT@VT Accessing a value forecast = To find the Zip code: where = forecast[“Place’] code = where[“Zip”] or just code = forecast[“Place”][“Zip”] To find the Hour: when = forecast[“Date”][“Time”][“Hour”] (C) Dennis Kafura 2016 Slide 14
CT@VT Demonstration n n Mapping the earthquakes data stream Using Spyder’s variable explorer (C) Dennis Kafura 2016 Slide 15
CT@VT Mapping diagram (C) Dennis Kafura 2016 Slide 16
CT@VT To do n n Work on class assignments Self-assess q Are you solid on the concepts? n q Are you up to date on homework? n q If not, see me If not, catch up Have you identified your project data stream and questions? n If not, see me and work on this (C) Dennis Kafura 2016 Slide 17
- Slides: 17