CS108 Lecture 19: Data Collections: Dictionaries Aaron Stevens 4 - - PDF document

cs108 lecture 19 data collections dictionaries
SMART_READER_LITE
LIVE PREVIEW

CS108 Lecture 19: Data Collections: Dictionaries Aaron Stevens 4 - - PDF document

CS108 Lecture 19: Data Collections: Dictionaries Aaron Stevens 4 March 2008 1 Overview/Questions Review: lists and list operations Associative data relationships, key-value pairs, examples. The Python dictionary ADT. 2 1


slide-1
SLIDE 1

1

1

Aaron Stevens

4 March 2008

CS108 Lecture 19: Data Collections: Dictionaries

2

Overview/Questions

– Review: lists and list operations – Associative data relationships, key-value pairs, examples. – The Python dictionary ADT.

slide-2
SLIDE 2

2

3

Review: Python lists

The Python list is a linear data

  • collection. Elements are stored in the
  • rder they are added.

The list support typical sequence

  • perations, including indexing, slicing,

and iteration.

4

Associative Data

Some data is not well suited to list storage/manipulation. Example: stock prices Huh? Prices for which stocks?

39.73 40.62 47.83 71.41

slide-3
SLIDE 3

3

5

Example: Stock Quotes

Common Stocks (shares of ownership in publicly traded companies) are often referred to by the ticker symbol, and a share has a price in dollars. Neither element has much meaning by itself.

Symbols: Prices:

“BAC” “EQR” “MO” “PCL” 32.51 38.24 23.24 19.32

6

Example: Stock Quotes

Data is given meaning by a relationship called a mapping, where an key from one domain is associated with a value from another domain.

Symbols: Prices:

32.51 38.24 23.24 19.32 “BAC” “EQR” “MO” “PCL”

slide-4
SLIDE 4

4

7

Associative Data Relationship

Data is stored by a relationship called a mapping, where an key from one domain is associated with a value from another domain. Sometimes data is called a key-value pair. Other examples:

– Name : Phone Number – Course : Grade

8

Python Dictionaries

A map or dictionary is an associative collection of key-value pairs. Python provides a built-in type dict, which is a dictionary.

# creating an empty dictionary stockPrices = {}

slide-5
SLIDE 5

5

9

Key-value Indexing

Elements in the dictionary are accessed by key-value indexing. For inserting/updating:

<dict>[<key>] = <value>

For retrieving:

<value> = <dict>[<key>] <key>,<value> can be of any type.

10

Key-value Indexing

stockPrices = {} # an empty dictionary stockPrices['BAC'] = 23.24 stockPrices['EQR'] = 32.51 stockPrices['MO'] = 19.32 stockPrices['PCL'] = 38.24 print stockPrices['BAC'] print stockPrices

slide-6
SLIDE 6

6

11

dicts are Sequences

Python dictionaries are sequences, which means usual sequence operations apply:

Reads a <value> out of a <dict>, assigns to var.

<value> = <dict>[<key>]

x takes on each tuple (key-value pair) in the sequence.

for x in <dict>.items()

x takes on each value in the sequence.

for x in <dict>.values()

Assigns a <value> to be mapped to a <key>.

<dict>[<key>] = <value>

Meaning Operation

x takes on each key in the sequence.

for x in <dict> for x in <dict>.keys()

12

dicts are Objects

Python dictionaries are objects, which supply some useful methods:

Deletes the pair corresponding to <key>. del <dict>[<key>] Deletes the entire collection of <key>-<value> pairs. <dict>.clear() Returns the <value> corresponding to <key>. <dict>.get(<key>)

Meaning Method

Returns a list of tuple s (<key>, <value>). <dict>.items() Returns a list of all <value> s. <dict>.values() Returns a list of all <key>s. <dict>.keys() Returns True if dictionary contains this key. <dict>.has_key(<key>) <key> in <dict>

slide-7
SLIDE 7

7

13

Data Constraints

Note: duplicate keys are not allowed.

– What happens if we try to add another mapping with an existing key? – What about duplicate values? The python dict is a uni-map. Python does not provide a multi-map, but we could write our own.

14

Example: wordcount.py

Let’s write a program which counts the

  • ccurrences of words in a text file.

– Create a mapping of _______ to _______ – Open a text file for reading – For each line in the file, create a list of words

  • For each word in the list, add to its count in mapping

– For all keys in mapping, print values (counts)

The work we write to process a dict of counts will be general – they will work an any data – not just words!

slide-8
SLIDE 8

8

15

Wordcount.py

16

Checking for existing key

Another approach, same effect:

slide-9
SLIDE 9

9

17

Ordering by Values

Normally the sort works by key. This function helps count by value:

18

Print out Word Counts:

slide-10
SLIDE 10

10

19

Take-Away Points

– Aassociative data relationship – Python’s built-in type dict – Key-value relationships – Uni-map – Multi-map

20

Student To Dos

– Reading: chapter 11.6 (today) – Lab 08 will be on on list/dict operations and data processing. – HW08 is posted now – Readings for after spring break: SQL Tutorial http://www.firstsql.com/tutor.htm