''' Purpose: demonstrate dict mappings ''' # get access to url support import url # where is the dog to notoriety mappings PUPPIES_CSV = 'http://www.cs.virginia.edu/~cs1112/datasets/csv/puppies.csv' # get data set dataset = url.get_dataset( PUPPIES_CSV ) # convert dataset to a form suitable for querying notoriety = { } for row in dataset : # Loop through each row in the dataset (each row contains # like Brian Griffin, Family Guy so it'll take that row name, fame = row # break up the row and assign it to be the key and value notoriety[ name ] = fame # Create a mapping of that name to that fame # So in our dictionary we would have notoriety{"Brian Griffin" : '"Family Guy"} print( notoriety ) # Print our dictionary once we've created all the mappings # After we looped through each row, got the key and value from the row, and got the # mapping and added it to our dictionary. # ask for dog of interest reply = input( 'Enter name of dog: ' ) dog = reply.strip() dog = dog.capitalize() # look up claim to fame fame = notoriety.get( dog ) # Remember the .get(key) function? # We got the name of a dog and we said that fame is assigned the # value of what the function .get( dog ) returns for the name of a # dog (key) in our dictionary # So we give it a dog (key) and we get its fame (value) # If you're still confused about this remember that # if we have dictionary d # d.get(key) will return the value mapped to that key # So when we said notoriety.get( dog ), it went into our dictionary # and got the value (the fame) mapped to that dog # If it's not a dog in our dictionary, it gets None print( fame )