CIT 590 Intro to Programming Lecture 6 (dictionaries)

Slides:



Advertisements
Similar presentations
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Advertisements

PHP I.
Container Types in Python
CIT 590 Intro to Programming Classes. Schedule change The upcoming HW (HW6) is your last major Python HW. It will involve object oriented programming.
Chapter 9: Advanced Array Manipulation
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
CIT 590 Intro to Programming Lecture 7. Agenda Configuring IDLE (now that your code is getting huge) Exceptions Testing for exceptions – the weird self.assertRaises.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Sequences The range function returns a sequence
Lists Introduction to Computing Science and Programming I.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Guide To UNIX Using Linux Third Edition
Factory Design Pattern, cont’d COMP 401 Fall 2014 Lecture 13 10/2/2014.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
CIT 590 Intro to Programming Style Classes. Remember to finish up findAllCISCourses.py.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
Memory organization - storing variables efficiently in the RAM memory.
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.
Runtime Environments Compiler Construction Chapter 7.
Physical DB Issues, Indexes, Query Optimisation Database Systems Lecture 13 Natasha Alechina.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
CIT 590 Intro to Programming Lecture 4. Agenda Doubts from HW1 and HW2 Main function Break, quit, exit Function argument names, scope What is modularity!
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
Built-in Data Structures in Python An Introduction.
CIT 590 Intro to Programming Lecture 5 – completing lists.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Lecture 04 – Models of memory Mutable and immutable data.
PHP vs. Python. Similarities are interpreted, high level languages with dynamic typing are Open Source are supported by large developer communities are.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
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.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
ISBN Chapter 10 Implementing Subprograms.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
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.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Python Programing: An Introduction to Computer Science
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1.
CS4432: Database Systems II
CIT 590 Intro to Programming Lecture 4. Random utilities to improve efficiency Search everything! Beyond Compare Keyboard shortcuts Not essentially but.
Accumulator Recursion CS125 Spring 2007 Arthur Kantor.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Dictionaries. The compound types you have learned about - - strings, lists, and tuples – use integers as indices. If you try to use any other type as.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
10 - Python Dictionary John R. Woodward.
Containers and Lists CIS 40 – Introduction to Programming in Python
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Guide to Programming with Python
Intro to Programming Lecture 5
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
CHAPTER 4: Lists, Tuples and Dictionaries
Linked Lists Chapter 5 (continued)
Presentation transcript:

CIT 590 Intro to Programming Lecture 6 (dictionaries)

Agenda Dictionary

Dictionaries consist of key, value pairs Also know as Hashmaps, associative arrays in other languages Initialized with d = {} Do not use the variable name dict. ‘reserved’ words Very efficient way of storing sparse data A lot of matrices and vectors that come up in probability are sparse, so you could use an integer key and store values in that manner

Simple operations The standard len and del work Loops work over the keys of the dictionary Dictionary.get()

Dictionary update operation. Copying dictionaries Dictone = [‘abc’: 3, ‘def’:7, ‘xyz’: 9} Dicttwo = [‘def’: 5, ‘pdq’ : 4} Dictone.update(dicttwo) As with lists or for that matter any kind of assignment, you need to be careful when you do assignment, since that is done by reference So dict1 = dict2 will make both dictionaries refer to the same value If you want to make what is generally called a ‘shallow copy’, you need to use the copy method Dict 2 = dict1.copy()

Initializing a dictionary version duex The dict() function can convert a list of 2 element tuples into dictionaries Very useful when you want to initialize a dictionary by using 2 lists, one of which has keys = [‘Arvind’, ‘Aashish’] values = [33, 28] dict(zip(keys, values))

Looping over a dictionary For e in dict: This will loop over they keys For e in dict.values(): As is somewhat obvious, this will loop over the values Since there is no guarantee about what order the keys are looped over, you might sometimes want to call the sorted function

Shallow copy versus deep copy Difference comes up in compound objects A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original You can manage to do both on a dictionary if you import the copy module Copy.copy = shallow copy Copy.deepcopy = deep copy Example with matrices

Simple program samples duesProgram.py The second optional argument for get can be exploited in interesting ways Useful for maintaining records Concordance.py

Altering dictionaries by passing them into functions Remember that arguments are passed into functions by reference, so you could get some unintended consequences if you are not careful Similar to our sneakyAppend examples in lists The debugger can be extremely useful here since it will always show you the complete dictionary

Dynamic programming The central principle is that you delay the computation of the function as much as possible When you compute a result, store it somewhere in memory Retrieve the results from memory Watch recursive fibonacci suddenly not become such a beast. Yes, this does assume we have memory (for a lot of cases this is ok)

Persistent variables The shelve module allows data to persist without using explicit file commands Data persists in a dictionary! import shelve Data = shelve.open(“database”) #and this will make an implicit file called database And then data from that point can basically be treated just as any other dictionary Remember to close the shelf before quitting by using Data.close() Files without really using files

The internal dictionaries Locals() gives you the local variables Correspondingly global() gives you the global variables

Summary Read chapter 5 in its entireity