''' 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 ) # returns list of lists # [[],[],[]] # ['Asta','The thin man'], ...] # convert dataset to a form suitable for querying # {'Asta':'The thin man', ...} # notoriety = { } # Initialize empty dictionary # for row in dataset: # look at each smaller list [k,v] (or the [dog, notoriety]) # dog_name, dog_fame = row # Take the list [dog, fame] and assign them to variables # notoriety[dog_name] = dog_fame # Create a mapping in my dictionary notoriety = url.get_dictionary(PUPPIES_CSV ) # url.get_dictionary(link) can convert # a csv on the web to a dictionary print('Notoriety dictionary=', notoriety ) # 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 ) # dictionary.get( key ) -> gives you value for key # gives you None if key is not in dictionary print( fame )