Create a Python dictionary where the value is a list. The key can be whatever type you want.
Design the dictionary so that it could be useful for something meaningful to you. Create at least three different items in it. Invent the dictionary yourself. Do not copy the design or items from some other source.
Next consider the invert_dict function from Section 11.5 of your textbook. (https://www.dropbox.com/s/gg6szmo42tfezl5/Think%20Python%202e.pdf?dl=0)
# From Section 11.5 of:
# Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree Press.
def invert_dict(d):
inverse = dict()
for key in d:
val = d[key]
if val not in inverse:
inverse[val] = [key]
else:
inverse[val].append(key)
return inverse
Modify this function so that it can invert your dictionary. In particular, the function will need to turn each of the list items into separate keys in the inverted dictionary.
Run your modified invert_dict function on your dictionary. Print the original dictionary and the inverted one.
Include your Python program and the output in your Learning Journal submission.
Describe what is useful about your dictionary. Then describe whether the inverted dictionary is useful or meaningful, and why.

Respuesta :

Answer:

def invert_dict(d):

   # Create a dictionary to store the inverse.

   inverse = dict()

   # Start the loop to traverse the dictionary.

   for key in d.keys():

       # Store the list of values stored at the current key.

       values = d[key]

       # Start the loop to traverse the list of values.

       for value in values:

           # Create a new key if the value is not

           # in the inverted dictionary.

           if value not in inverse:

               inverse[value] = [key]

           

           # Otherwise, append the value to the current key.

           else:

               inverse[value].append(key)

   

   # Return the inverted dictionary.

   return inverse

# Define the main() function.

def main():

   dic = {'a': ["aegon", "archangel"], 'b': ["blackbolt"]}

   # Display the original dictionary.

   print("Original dictionary:")

   print(dic)

   # Call the method and display the inverted dictionary.

   print("\nInverted dictionary:")

   print(invert_dict(dic))

# Call the main() function.

if __name__ == "__main__":

   main()

Modification Part:

The inverted dictionary is not useful since the basic idea of a dictionary is to map key-value pairs. All the common attributes of the different values are mapped to the key. Ideally, it is assumed that value is mapped to only one key.

The inverted dictionary will have inverted key-value pairs. As seen above, the value is the same for all the keys that are made out of the list of values from the original key-value pair. For example, the keys ‘aegon’ and ‘archangel’ contains the same value ‘a’ since they are created from the list of values of the same key.

The keys of the inverted dictionary are no longer useful as most of the keys will have the same value if there is a large list mapped to a single key in the original dictionary.

Explanation:

ACCESS MORE