''' 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 # another accumulation - building dictionary notoriety = {} # start with empty dict --> comes with all its functions # { } == {} # whitespaces are ignored for row in dataset: # for each row in the dataset # each row contains a dog's name and its importance: [ dog_name, importance ] # row has 2 elements ( we know this ) --> put two variables on the left --> assign each of the elements # into each of the variables # the first element in the list row == name of dog # the second element in the list row == importance of the dog name, importance = row # not splitting in the sense that you use .split() on strings # string.split() --> turns string into a list # row is a list, cannot split it like you split with strings # ex: # name, importance = [ 'snoopy', 'good boy' ] # name = 'snoopy' # importance = 'good boy' # same thing as above name = row[0] importance = row[1] # add to the dictionary (no append, b/c not a list) # dict[ key ] = value notoriety[ name ] = importance # 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 ) # given a dog name, we want to get the importance print( fame ) print() # functions keys() and values() are dict functions # dict_name.keys() dict_name.values() notoriety_keys = notoriety.keys() # gives us all the keys in the dictionary - all unique values # not a list notoriety_values = notoriety.values() # gives us all the values in the dictionary - may not be all unique # also not a list # to convert the two types above into a list, we use the list() function list_of_notoriety_keys = list( notoriety_keys ) list_of_notoriety_values = list( notoriety_values ) print( list_of_notoriety_keys ) print( list_of_notoriety_values ) # print( notoriety_keys ) # print( notoriety_values )