1 Working With Dictionaries
1.1 Start with solving the Assessment posed yesterday
1.2 Completing the Dictionary concept
1.3 Deleting elements from a dictionary
1.4 Homework 5 Review
1.5 Debug Challenge
1.6 Self Assessment
1.7 Version : 2014/ 02/ 19

CS 110X Feb 14 2014

Lecture Path: 16
Back Next

Expected Reading: TBA
Expected Interactions: Iterative over a dictionary
Clicker: Assessment Today

HW5 Ready: Assignment Template SacramentocrimeJanuary2006.csv Sacramentorealestatetransactions.csv sacramentoInverted.jpg smallDataSet.csv


If you have a big enough dictionary, just about everything is a word.
Dave Barry

1 Working With Dictionaries

1.1 Start with solving the Assessment posed yesterday

How would we solve this problem? Consider asking the four questions

Given a list of numbers and a target value, return an element such that no other element in the list is closer to the target.

Let’s solve together.

1.2 Completing the Dictionary concept

Yesterday you were introduced to dictionaries. This lecture will complete the presentation.

A dictionary can store an arbitrary number of (key, value) pairs. Traditionally, as mentioned in class, all the keys are of the same type and all the values are of the same type.

>>> library = {} >>> library[’978-0439023528’] = ’The Hunger Games’ >>> library[’978-0439023511’] = ’Mockingjay’ >>> library[’978-0545586177’] = ’Catching Fire’

Here you use a dictionary whose keys are string literals (ISBN numbers, actually) and whose values are the titles of the books. You could use this to represent the books you own.

Instead of writing a program to manually enter each of your books, you would rather create a comma-separated file containing your book collection.

Assume you have taken the time to produce such a record.

Start with the extractAllRecords method we introduced yesterday.

def extractAllRecords(fileName): """ Extract all CSV records and return as single LIST-of-RECORDS Note that the first element in this list contains the description of the columns as defined in the CSV file """ file = open (fileName, ’r’) reader = csv.reader(file) results = [] for row in reader: results.append(row) file.close() return (results)

Using this function, you could write the following function which would load up your library collection and use a dictionary to store the books. The keys in the dictionary would be the ISBN number, and the value would be the entire row, which represents all information for that book. Note that I am assuming you have the following function in the same module as the above function.

libraryName = ’books.csv’ myLibrary = {} def loadLibrary(): """ Load up library from books.csv file and return total number of books found """ mybooks = extractAllRecords(libraryName) for index in range(1, len(mybooks)): row = mybooks[index] isbn = row[1] library[isbn] = row return len(library)

Note that because ISBN is a unique value, you are guaranteed to store all of the books in a single dictionary.

With this structure in place, you can then query the library to find books by their ISBN number.

1.3 Deleting elements from a dictionary

Once you have a dictionary object, you can add new (key, value) associated pairs. But can you remove an associaed pair? Yes! To do this, use the del keyboard provided by Python.

To delete an entry from a dictionary, act as follows:

>>> tv = {} >>> tv[4] = ’CBS’ >>> tv[5] = ’ABC’ >>> tv[13] = ’FOX’ >>> tv {4: ’CBS’, 5: ’ABC’, 13: ’FOX’} >>> del tv[5] >>> tv {4: ’CBS’, 13: ’FOX’}

You can replace the value associated with a key simply by requesting to store the new value with the original key.

>>> tv = {} >>> tv[4] = ’CBS’ >>> tv[5] = ’ABC’ >>> tv[13] = ’FOX’ >>> tv {4: ’CBS’, 5: ’ABC’, 13: ’FOX’} >>> tv[4] = ’WGBH’ {4: ’WGBH’, 5: ’ABC’, 13: ’FOX’}

1.4 Homework 5 Review

In class we need to review the concept of Lists of Lists and make sure you understand how to process these structures.

Demonstrations in class using the Sacramento data sets...

1.5 Debug Challenge

The following function attempts to create a list of lists for the entries in a dictionary.

Press To Reveal

def expand(collection): for element in collection: result.append(collection[element]) def main(): collection = {} collection[5] = ’holiday’ collection[11] = ’work’ mylist = expand(collection) print (mylist)

That is, the value that it wants to print out is:

[[5, ’holiday’], [11, ’work’]]

However instead, it doesn’t even run. What is going wrong?

Defects

1.6 Self Assessment

Given two lists of values, return a new list which contains all values in both lists without including any duplicates.

Here is sample output:

>>> uniq([1, 4, 5, 9], [2, 3, 4, 5, 6]) [1, 4, 5, 9, 2, 3, 6]

1.7 Version : 2014/02/19

(c) 2014, George Heineman