cs108 lecture 19 data collections dictionaries
play

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


  1. 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

  2. Review: Python list s The Python list is a linear data collection. Elements are stored in the order they are added. The list support typical sequence operations, including indexing, slicing, and iteration. 3 Associative Data Some data is not well suited to list storage/manipulation. 39.73 Example: stock prices 40.62 47.83 71.41 Huh? Prices for which stocks? 4 2

  3. 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: 32.51 “BAC” 38.24 “EQR” 23.24 “MO” 19.32 “PCL” 5 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 “BAC” 38.24 “EQR” 23.24 “MO” 19.32 “PCL” 6 3

  4. 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 7 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 = {} 8 4

  5. 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. 9 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 10 5

  6. dict s are Sequences Python dictionaries are sequences , which means usual sequence operations apply: Operation Meaning x takes on each key in the sequence. for x in <dict> for x in <dict>.keys() x takes on each value in the sequence. for x in <dict>.values() x takes on each tuple (key-value pair) in the for x in <dict>.items() sequence. Assigns a <value> to be mapped to a <key> . <dict>[<key>] = <value> Reads a <value> out of a <dict> , assigns to <value> = <dict>[<key>] var. 11 dict s are Objects Python dictionaries are objects , which supply some useful methods: Method Meaning Returns True if dictionary contains this key. <dict>.has_key(<key>) <key> in <dict> <dict>.keys() Returns a list of all <key> s. Returns a list of all <value> s. <dict>.values() Returns a list of tuple s ( <key> , <value> ). <dict>.items() Returns the <value> corresponding to <key> . <dict>.get(<key>) Deletes the pair corresponding to <key>. del <dict>[<key>] Deletes the entire collection of <key> - <value> <dict>.clear() pairs. 12 6

  7. 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. 13 Example: wordcount.py Let’s write a program which counts the occurrences 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! 14 7

  8. Wordcount.py 15 Checking for existing key Another approach, same effect: 16 8

  9. Ordering by Values Normally the sort works by key. This function helps count by value: 17 Print out Word Counts: 18 9

  10. Take-Away Points – Aassociative data relationship – Python’s built-in type dict – Key-value relationships – Uni-map – Multi-map 19 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 20 10

Recommend


More recommend