DOC PREVIEW
TAMU CSCE 110 - Dictionary
Type Lecture Note
Pages 4

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSCE 110 1nd EditionLecture 19Outline of Last Lecture:I. Review of basic propertiesA. List propertiesB. Set propertiesOutline of Current Lecture:I. DictionaryA. Introduction and basic propertiesB. Incorporating a dictionary in programmingCurrent Lecture:I. DictionaryA. What are dictionaries in Python? A dictionary is a set of key value points, each key being corresponding with an associated value. Keys are to words in the dictionary as associated valuesare to definitions in the dictionary. These keys can be strings, tuples, integers/floats, but they must all be unique. This means that each key in the dictionary has only one value associated with it (however, a different key can have the same value as another key). Keys are immutable, meaning that once a key is established, it cannot be changed. The values are mutable, and can be any type of value. Dictionaries are also similar to sets, in that the order of the values in the dictionary is arbitrary - it either is or isn't in the dictionary. The keys and values in the dictionary are joined with ":", and a dictionary is denoted with {}. As in a list or set, the different key/value elements are separated with a comma. Let's look at an example in Python Shell, output being in green, and explanations in italics:>>> x = {'e':4012, 'm':1655, 'h':6718}Here, we initialize a dictionary and give it the keys and values of our choosing. >>> x{'m': 1655, 'e': 4012, 'h': 6718}We simply call back our dictionary, and as you can see, the keys and values remain the same, but the order has changed, which means nothing to us.>>> x['e'] += 1To change the value of a key, we use the same concept as we did in lists. We call the key of the dictionary we wish to change (like calling an index of a list), and we give it a new value, which does not change the key at all. We are just increasing the value associated with 'e' by 1. >>> x{'m': 1655, 'e': 4013, 'h': 6718}The next time we call our dictionary, we can see that the value of 'e' has increased by 1. These notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.>>> x['i'] = 1If we want to add a key to the dictionary, we simply call the key (which does not exist until now) and give it the value of our choosing. >>> x{'i': 1, 'm': 1655, 'e': 4013, 'h': 6718}As we can see, the length of our dictionary (now 4) has increased by one and the new key/value pair is present.>>> x['m']1655We can call values of the dictionary in a way similar to with respect to lists.B. Programming with dictionariesWe will look at a program that utilizes the dictionary, using the same text file containing a wide range of five-letter-words. Each word begins with a letter denoting the difficulty of the word: h:aaliim:aarghm:abacam:abacie:abackh:abacsh:abada... and so on.Our first program will contain several different functions, all that serve to count the number of words in each category: easy, medium, and hard. In these types of programs, the dictionary comes in handy.1 def count_difficulty(words):2 easy = 03 medium = 0 4 hard = 0 5 for word in words:6 word = word.split(':')7 if word[0] == 'e':8 easy += 19 elif word[0] == 'm':10 medium += 111 else:12 hard += 1 13 return easy, medium, hard14 #This is the first example of how we can count the number of words that are easy, medium, 15 #and hard, which doesn't even utilize dictionaries or their properties. You should know 16 # enough from previous notes to understand how this one works, so let's move on.1718 def count_difficulty2(words):19 difficulty_dict = {} 20 for word in words:21 word = word.split(':')22 key = word[0]23 if key in difficulty_dict:24 difficulty_dict[key] += 125 else:26 difficulty_dict[key] = 1 27 return difficulty_dict['e'], difficulty_dict['m'], difficulty_dict['h']28 #Here, we use a dictionary. We use a for loop to look at each word separately until we have 29 #checked each one. In line 22, we initialize a local variable which will have the value as the 30 #first letter of each word (this will be the difficulty, e, m, or h). In lines 23-26, it checks to see 31 #if the difficulty encountered is already in the dictionary. If it is, it increases the value by 1, as 32 #it is counting the number of words with that difficulty, and if not, it adds the difficulty as a 33 #new key and gives it the value 1, as it has found the first word with that difficulty. 3435 def count_difficulty3(words):36 difficulty_dict = {} 37 for word in words:38 word = word.split(':')39 key = word[0] 40 difficulty_dict[key] = difficulty_dict.get(key, 0) + 1 41 return difficulty_dict['e'], difficulty_dict['m'], difficulty_dict['h']42 #Here is the shortest function of all. Lines 23-26 from the previous function are replaced by 43 #one line, line 40. This is a new concept that makes our lives much easier. We use the built-in 44 #"get" function. This function has two arguments. The first ("key" in the parentheses) is the 45 #value that we check to see if it's already in the dictionary. If it is, it doesn't look at the 46 #second variable at all, and carries on (in our case, it moves on to the "+1"). If the value is not 47 #already in the dictionary, it does look at the second argument. It adds the new key to the list 48 #and gives it the value of the second argument (in this case, 0) and then carries on, adding 1.4950 def main():51 open_file = open('five-letter-words.txt')52 words = open_file.readlines()53 easy, medium, hard = count_difficulty3(words)54 print 'Difficulty Level Counts'55 print ' easy:', easy56 print ' medium:', medium57 print ' hard:', hard58 main()The main function simply opens the file and sends it to one of our three functions that counts the number of words in each difficulty. The values are then printed. In this case, we have chosen to use the third function, but any of them will


View Full Document

TAMU CSCE 110 - Dictionary

Type: Lecture Note
Pages: 4
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download Dictionary
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Dictionary and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Dictionary 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?