ecs10-11-05 - 11/5/2008 Announcements ECS 10 11/5 I have...

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: 11/5/2008 Announcements ECS 10 11/5 I have the scantrons from the midterm. have You You can come look at them in my office. I will be in my office during my office hour 2-3pm 2today also 5pm today, also 3-5pm. Homework Homework solutions on myUCDavis soon. The The Computer When When we read a file, we get data off the disk and put it into variables in memory. Memory Memory Memory hardware is one big list. position files processor memory disk data Variables Python Python remembers a position for each variable name. List in Memory Python Python remembers first position of list. When When we say petList petList[3], it adds it three three to the first position. It It goes right to the correct box; does not read whole list. petList[0] petList petList[3] position data balance x 1 11/5/2008 Dictionaries The The memory hardware is really a list. There There is no dictionary hardware; Python has to fake it using a list. Uses a classic CS trick called “hashing” CS “Hash “Hash function” – takes a string and turns it into an integer that can be used as an index. Computing Computing Hash hash = 0 dictLength = 100 for char in string: hash = (hash+ord(char)*61) % dictLength print hash The The 61 is there to make sure two similar strings hash to very different integers. Could Could have used 71, 77…. study number theory! Dictionary in Memory Python Python remembers first position petDict[0] dictionary. When When we say petDict[54] petDict[‘dog’], petDict[‘dog’], it hashes ‘dog’ to get an integer, maybe 54. Then Then it gets data from petDict[54]. petDict[54]. Dictionary in Memory At At position petDict[54], petDict[54], Python petDict[0] stores the key-value keypair dog pair ‘dog’:’RinTinTin’. petDict[54] Notice Notice it does not have to read the whole dictionary to get an item in or out. …. …. Problem What What if two strings hash to the same index? There There are lots of ways this problem has been attacked, different solutions. Some of my research now is about how to do it it on graphics cards. Under Under the Hood This This is how dictionaries really work. No No magic at all is involved. You You don’t need to know how dictionaries work to use them. But But maybe it helps? Hashing Hashing is a great trick you might be able to use someday. 2 11/5/2008 Variables “Python “Python remembers a position for each variable name” StringString-positions are…key-value pairs. are…keyPython has a dictionary inside it, which it uses to to look up the positions corresponding to each variable name. Getting Getting Keys Can Can get the list of keys in a dictionary using the keys method: petList = petDict.keys() Can Can use this in a for loop: for pet in petDict.keys(): print 'My',pet,'is named',petDict[pet] For Loop with a Dictionary for pet in petDict: print ‘My’,pet,’is named’,petDict[pet] In In the first line, the variable pet becomes on pet each of the keys in turn. In In the block under the for statement, we can for use pet as the key, to look up the values. pet as to The The keys come out in no particular order. 3 ...
View Full Document

This note was uploaded on 03/23/2011 for the course ECS 10 taught by Professor Amenta during the Winter '07 term at UC Davis.

Ask a homework question - tutors are online