ecs10-12-03 - 12/3/2007 Name Database Program Your program...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 12/3/2007 Name Database Program Your program should have two parts. ECS 10 12/3 Part 1: Read file and store data in a dictionary Part 2: Loop: Get name from user Draw a graph for that name Brings up a series of graphics windows, one after the other. Series of Graphics Windows Put up a graphics window, don't do anything until the user closes the graphics window. That is what the loop with the gr.sleep() gr.sleep() function does: does: alive = True while alive: alive = gr.sleep(.5) gr.sleep(.5) Your program should execute this loop for every graphics window that it brings up. Dictionaries Good for queries. Values in a dictionary are indexed by keys, which can be anything. Items i a di i I in dictionary are not i any particular in i l order. Looking up a key that is not in the dictionary causes an error; check first: if "sparrow" in D: print D["sparrow"] Lists Good when the order of the items matters. The items in a list are in a specific order. A list is always indexed by integers. Using an index which is bigger than the length of the list causes an error. Similar in Some Ways Can use len() with both. len() print len(D) # Length of dictionary D print len(L) # Length of list L Can use either one in a for loop! 1 12/3/2007 For Loop with a Dictionary for bird in D: print bird,D[bird] In the first line, the variable bird becomes on each of the keys in turn. In the block under the for statement, we can use bird as the key, to look up the values. The keys come out in no particular order. Example Problem Say I have a file of SID numbers and names. 577837224, "Oswald, Astrid" 578364002, "Ortiz, Esteban" I want to write a program that will let me enter an SID, and get back the name. SID is an integer, name is a string. Possible Data Structures Which would be a better data structure: A list of names, indexed by SID number A dictionary, using the SID numbers as keys A list of lists [SID,name] [SID,name] Problem with List of Lists Might have to read the whole list to find the SID we want. So it is slow at answering queries (not a problem with 188 people, but would be with 50 000) people 50,000). Problem with List indexed by SID Many possible SID numbers won't have a corresponding person If this does not cause an error: L[578364002] then the length of the list has to be >= 578364002, and if we use any smaller index: L[346876206] that should have some value too (maybe the empty string?) So we need millions of items in the list Dictionary is Best ...for this problem, anyway. Length of dictionary is the number of items in it, not the size of the bi h i f h biggest k key. You can access items using the key, not by looking through the whole data structure. Even though the keys are integers, if lots of possible keys are not used, then a dictionary works best. 2 12/3/2007 Faking a Dictionary We'll fake a dictionary using a list This is a classic CS trick called "hashing" "Hash function" takes a big integer and turns it into a small one that can b used as an i d i i ll h be d index. Hash Function Converts a big integer into a small one. Can use the small one as an index into a small list. Under the Hood This is how dictionaries really work. You don't need to know this to use them. But maybe it helps? A good trick you might be able to use someday. Strings as Keys? This works for big integers, but how about strings? First turn string into big integer... then i h into a small i ll integer... which you use as an index! Basic idea: ord() function turns a character into ord() an integer. 3 ...
View Full Document

Ask a homework question - tutors are online