CIT 590 Intro to Programming Lecture 6 (dictionaries)

14
CIT 590 Intro to Programming Lecture 6 (dictionaries)

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

Page 1: CIT 590 Intro to Programming Lecture 6 (dictionaries)

CIT 590Intro to Programming

Lecture 6 (dictionaries)

Page 2: CIT 590 Intro to Programming Lecture 6 (dictionaries)

Agenda• Dictionary

Page 3: CIT 590 Intro to Programming Lecture 6 (dictionaries)

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

Page 4: CIT 590 Intro to Programming Lecture 6 (dictionaries)

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

Page 5: CIT 590 Intro to Programming Lecture 6 (dictionaries)

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

Page 6: CIT 590 Intro to Programming Lecture 6 (dictionaries)

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

Page 7: CIT 590 Intro to Programming Lecture 6 (dictionaries)

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

Page 8: CIT 590 Intro to Programming Lecture 6 (dictionaries)

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

Page 9: CIT 590 Intro to Programming Lecture 6 (dictionaries)

Simple program samples• duesProgram.py• The second optional argument for get can be exploited in

interesting ways• Useful for maintaining records• Concordance.py

Page 10: CIT 590 Intro to Programming Lecture 6 (dictionaries)

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

Page 11: CIT 590 Intro to Programming Lecture 6 (dictionaries)

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)

Page 12: CIT 590 Intro to Programming Lecture 6 (dictionaries)

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

Page 13: CIT 590 Intro to Programming Lecture 6 (dictionaries)

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

Page 14: CIT 590 Intro to Programming Lecture 6 (dictionaries)

Summary• Read chapter 5 in its entireity