ecs10-11-3

ecs10-11-3 - Current Assignment ECS 10 11/3 Two Two weeks...

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: Current Assignment ECS 10 11/3 Two Two weeks long Hand Hand in preliminary version Wds, Nov. 5. Wds, Uses Uses a dictionary Uses Uses the two-loop structure in which one twoloop builds a data structure, and the other uses it. Foreclosure Foreclosure Map Overall Project Plan Read Read census data. Build a data structure that lets us look up the population of a county. Read Read the foreclosure data. For each county: Look up its population in data structure Compute Compute ratio Write Write out line of table for Many Eyes Upload Upload completed table to Many Eyes and view visualization. Structure Structure of program Very Very typical program structure: while loop to read file put some data into a data structure loop to do something with data remove data from data structure produce some output Program Evolution Start Start with a simple program that does only part of what you need. Change Change it so it does a little bit more. Only when this works, change it again. when change Always Always have a partially working program. See See bugs one at a time. Explicit Explicit directions for how to evolve program on Web page. Hand Hand in partially working program on Wds. Wds. 1 Dictionary Words Words are keys keys. Definitions Definitions are values are values. Dictionary Dictionary repDict = {‘Pelosi’: 8, ‘McNerney’: 11 } ‘McNerney’: key value Curly Curly braces {} indicate a dictionary Usually, Usually, assign empty dictionary: repDict = {} Adding an Item Look Look up a value, use key as index. repDict[‘Pelosi’] repDict[‘Pelosi’] = 8 key value Adding an Item Look Look up a value, use key as index. repDict[‘Pelosi’] repDict[‘Pelosi’] = 8 Variable = Expression Use Use square brackets for indexing, just like a list. Looking up an Item Look Look up a value, use key as index. district = repDict[‘Pelosi’] repDict[‘Pelosi’] value key Looking up an Item Look Look up a value, use key as index. district = repDict[‘Pelosi’] repDict[‘Pelosi’] Variable = Expression 2 Naming variables The The variable is the label on the box. If If it describes what is inside, it is easier to understand what the program is doing. So I call my dictionary of representatives ll repDict. repDict. Dictionaries with the in operator Looking Looking up a key that isn’t there causes the program to crash! So So always use the in operator to check and in see see if a key is in the dictionary before looking it it up. if ‘Amenta’ in repDict: district = repDict[‘Amenta’] The word ‘in’ in Python Means Means different things in different contexts. In In a for loop…. for beast in [‘cow’, ‘dog’, ‘bat’]: print beast The variable beast will contain each of the elements of the list in turn. The word ‘in’ in Python With With a string…. if ‘oman’ in ‘romania’: print ‘Found Oman in Romania!’ Boolean, Boolean, is True if substring on the left is part of the string on the right. The word ‘in’ in Python With With a list…. if ‘pig’ in [‘cow’, ‘dog’, ‘rat’]: print ‘Found the pig.’ Boolean, Boolean, is True if data item on the left is an element of the list on the right. The The word ‘in’ in Python With With a dictionary…. if ‘Pelosi’ in repDict: district = repDict[‘Pelosi’] Boolean, Boolean, is True if data item on the left is key in the dictionary on the right. Use Use it to check and see if a key is there. 3 Dictionary Values can be Lists We We make more complicated data structures by combining the ones we have. For For instance: cityDict = {} cityDict[‘Mexico’] = [‘Mexaco DF’, ‘Veracruz’] print cityDict[‘Mexico’] The value is a list. It is a dictionary of lists. Using Using the dictionary of lists country = ‘France’ cities = cityDict[country] for city in cities : print city,’is a city in’,country cities is a list of strings city Is a string Beautify repQuery Put Put whole name as well as district into the dictionary. The The value None Sometimes Sometimes you want to make a variable, but not put anything in it. Like Like making a box, putting a label on it, but not not putting anything inside. Not Not the empty list, not the empty string; it does not have a type. Using Using None reply = None while reply != '': reply = raw_input('Enter a CA Congressional \ District, or enter to exit: '') ) if reply != '': district = int(reply) 4 ...
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