Question
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following:
- How to format each dictionary item as a text string in the input file.
- How to covert each input string into a dictionary item.
- How to format each item of your inverted dictionary as a text string in the output file.
Create an input file with your original three-or-more items and add at least three new items, for a total of at least six items.
Copy your program from Part 1 and modify it to do the following:
- Read the output file from Part 1 and create a dictionary from it (the inverted dictionary from Part 1).
- Invert that dictionary.
- Write the re-inverted dictionary to an output file.
It will be interesting to see if your original dictionary is reversible. If you invert it twice, do you get the original dictionary back?
Include the following in your Learning Journal submission:
- The input file for your original dictionary (with at least six items).
- The Python program for Part 1.
- The output file for your inverted dictionary, which is also the input file for Part 2.
- The Python program for Part 2.
- The output file for your twice-inverted dictionary.
- A description of any differences between your program for Part 1 and your program for Part 2.
- A description of any differences between the original input file and the final twice-inverted output file.
Learning Journal Unit 7
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.
# From Section 11.5 of:
# Downey, A. (2015). Think Python: How to think like a computer scientist . Needham, Massachusetts: Green Tree Press.
definvert_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.Step by Step Solution
3.41 Rating (148 Votes )
There are 3 Steps involved in it
Step: 1
code to copy definvertdictd inverse dict for key in d val dkey if typeval ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started