CIT 590 Intro to Programming Lecture 6 (dictionaries)

Post on 14-Dec-2015

217 views 2 download

Transcript of CIT 590 Intro to Programming Lecture 6 (dictionaries)

CIT 590Intro to Programming

Lecture 6 (dictionaries)

Agenda• Dictionary

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