Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Dictionaries

Similar presentations


Presentation on theme: "Introduction to Dictionaries"— Presentation transcript:

1 Introduction to Dictionaries
Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 Dictionaries

2 Digression — augmented assignment
count = count + 1 remaining = remaining – consumption speed = speed * factor dist = dist / 2 What do these have in common? Answer:– Update of a variable by a quantity The most basic form of increment or decrement Shortcut notation:– count += 1 remaining -= consumption speed *= factor dist /= 2 Somewhat like a contraction in English CS-1004, A-Term 2014 Dictionaries

3 Digression (continued)
Widely used notation in Python, C, C++, Java, and other languages Occasional subtle differences Example:– L[randrange(1,7)] = L[randrange(1,7)] + 1 Does not do the right thing! Should be:– idx = randrange(1,7) L[idx] = L[idx] + 1 This does work correctly in all cases:– L[randrange(1,7)] += 1 Evaluates randrange() only once! #to update correct list entry CS-1004, A-Term 2014 Dictionaries

4 Digression (concluded)
Augmented assignment operators:– += addition -= subtraction *= multiplication /= division //= integer division %= remainder **= exponentiation And some we have not used in this class:– >>= right shift <<= left shift &= bitwise AND ^= bitwise XOR |= bitwise OR CS-1004, A-Term 2014 Dictionaries

5 Questions? CS-1004, A-Term 2014 Dictionaries

6 Dictionary Important data type in Python Read and study §11.6
And all of Chapter 11! Definition:– “Dictionary” A Python data type for collections, capable of storing and retrieving key-value pairs, … … where keys and values can be any type, … … data is unordered! Called a hash table in most other languages Not a built-in data type CS-1004, A-Term 2014 Dictionaries

7 Lab 5 “Write the function PlayManyGames(n) needed by Homework #5.
This must call your previous function PlayOneGame(). For each call to PlayOneGame(), use the result to index into a list and increment the value stored that location in the list.” A hassle! Using indexing into list to store values not directly associated with indexes! Dice value 2  list[0] Dice value 3  list[1] Dice value 12  list[10] CS-1004, A-Term 2014 Dictionaries

8 Lab 5 and HW 5 (continued) Would really like something like:–
for i in range(2, 13): D[i] = 0 ... D[rollDice()] += 1 … or better yet:– D[playOneGame()] += 1 Remember that PlayOneGame() returns a pair (win/loss, # of throws) With dictionaries, we can do this! CS-1004, A-Term 2014 Dictionaries

9 Using Dictionaries Creating a dictionary Also called a “mapping”
Remember:– Lists = square brackets — [ ] Dictionaries = curly brackets — { } Adding an item to dictionary D[key] = value key may number, string, tuple i.e., anything ‘immutable’ value can be anything at all! D[key] acts like a variable Also called a “mapping” CS-1004, A-Term 2014 Dictionaries

10 Getting item from Dictionary
variable = D[key] Very fast, efficient “lookup” of key Uses technique in computing called “hashing” key is scrambled Scrambled value used to index into big list Easy to find by simple search from there Does not preserve order Organized internally for speed of access CS-1004, A-Term 2014 Dictionaries

11 Examples CS-1004, A-Term 2014 Dictionaries

12 Operators, functions, & methods
key in dict True if key is in the dictionary, False if not dict.keys() List of keys in the dictionary dict.values() List of values in dictionary dict.items() List of (key,value) pairs dict.get(key,default) Returns dict[key] if present, default otherwise del dict[key] Deletes key and dict[key] from dictionary for k in dict: Loop over all keys in the dictionary Internal order of storage CS-1004, A-Term 2014 Dictionaries

13 More Examples Questions? CS-1004, A-Term 2014 Dictionaries

14 What can we do with dictionaries
Store stuff! Easily So it is easily retrievable Organize data by different criteria Pull out non-numeric data CS-1004, A-Term 2014 Dictionaries

15 Example – Homework #4 dict is a dictionary of word-count pairs
Indexed by word Read file, split into words, strip, lower-case Just as in HW4 For each word of input If word in dictionary, increment dict[word] If not, dict[word] = 1 Get list of unique words uniqueWords = list(dict.keys()) Sort uniqueWords.sort() Get counts for word in uniqueWords(): write(dict[word], word) CS-1004, A-Term 2014 Dictionaries

16 Questions? CS-1004, A-Term 2014 Dictionaries


Download ppt "Introduction to Dictionaries"

Similar presentations


Ads by Google