140 likes | 244 Views
COMPSCI 101 Principles of Programming. Lecture 25 – Dictionaries. Learning outcomes. At the end of this lecture, students should be able to: Create a dictionary Retrieve items from a dictionary. Review. Lists are retrieved by indexes: >>> my_list = [1,2,3,4,5] >>> my_list [0] 1
E N D
COMPSCI 101Principles of Programming Lecture 25 – Dictionaries
Learning outcomes • At the end of this lecture, students should be able to: • Create a dictionary • Retrieve items from a dictionary COMPSCI 101 - Principles of Programming
Review Lists are retrieved by indexes: >>> my_list = [1,2,3,4,5] >>> my_list[0] 1 >>> my_list[-1] 5 >>> my_list[3] = 8 >>> my_list [1,2,3,8,5] COMPSCI 101 - Principles of Programming
Python Dictionaries • A dictionary is like a list, but more general. • In a list, the indices have to be integers; • in a dictionary they can be (almost) any type. • You can think of a dictionary as a mapping between • a set of indices (which are called keys) and • a set of values. • Each key maps to a value. • The association of a key and a value is called a • key-value pair or sometimes an item. COMPSCI 101 - Principles of Programming
Dictionary Examples >>> english2spanish = dict() >>> print (english2spanish) {} >>> english2spanish['one'] = 'uno' >>> print english2spanish {'one':'uno'} >>> english2spanish = {'one': 'uno', 'two': 'dos', 'three': 'tres'} >>> print english2spanish['four'] KeyError: ‘four’ >>> 'one' in english2spanish (this only works for keys) True COMPSCI 101 - Principles of Programming
Making an Address Book >>> address_book = dict() >>> address_book["Helen"] = "Helen Clark, New York City, 2231-12434-4534" >>> address_book["Helen"] 'Helen Clark, New York City, 2231-12434-4534' >>> address_book["HELEN"] KeyError: 'HELEN' COMPSCI 101 - Principles of Programming
Children’s word game A certain childrens game involves starting with a word in a particular category. Each participant in turn says a word, but that word must begin with the final letter of the previous word. Once a word has been given, it cannot be repeated. If an opponent cannot give a word in the category, they fall out of the game. For example, with "animals" as the category, Child 1: dog Child 2: goldfish Child 1: hippopotamus Child 2: snake ... COMPSCI 101 - Principles of Programming
Exercise Write a function named play_word_game() to play this game. Our function is not restricted to a single category. COMPSCI 101 - Principles of Programming
Playing the game >>> Pick the first word: cat This is my word: t Give next word (or q to quit): tat This is my word: t's Give next word (or q to quit): sat This is my word: ta Give next word (or q to quit): at This is my word: tab Give next word (or q to quit): bat This is my word: tabernacle Give next word (or q to quit): enter This is my word: r Give next word (or q to quit): rat This is my word: table Give next word (or q to quit): q >>> COMPSCI 101 - Principles of Programming
Answer defplay_word_game(): again = "y" dictionary = set_up_dictionary("unixdict.txt") used_words = [] word = input('Pick the first word: ') while again == "y": word_list= dictionary[word[-1]] new_word = find_word(word_list,used_words) print("This is my word: ",new_word) OK = "n" while OK == "n": word = input('Give next word (or q to quit): ') if word == 'q': return if word[0] == new_word[-1] and test_word(dictionary,word): OK = "y" else: print("Your word is illegal") COMPSCI 101 - Principles of Programming
Helper Functions defmake_first_letter_dictionary(dictionary_list): real_dictionary = dict() for word in dictionary_list: if word[0] not in real_dictionary: real_dictionary[word[0]] = [word] else: real_dictionary[word[0]] += [word] return real_dictionary defset_up_dictionary(dictionary_file): dictionary_file = open(dictionary_file, "r") dictionary = dictionary_file.read() dictionary_list = dictionary.split() real_dictionary = make_first_letter_dictionary(dictionary_list) return real_dictionary COMPSCI 101 - Principles of Programming
More Helper Functions deftest_word(dictionary,word): if word in dictionary[word[0]]: return True return False deffind_word(word_list,used_words): for word in word_list: if word not in used_words: used_words += [word] return word return "" play_word_game() COMPSCI 101 - Principles of Programming
Summary • How to create a dictionary • How to retrieve things from a dictionary • How to create a larger piece of code with many functions COMPSCI 101 - Principles of Programming
Tomorrow • More fun with Dictionaries and Histograms (counting Dictionaries) COMPSCI 101 - Principles of Programming