Part 1 Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file.
Part 1
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.
Part 2
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?
here is the data:
def invert_dict(d):
inverse = dict()
for key in d:
val = d[key]
for item in val:
inverse[item]=key
return inverse
hist = dict({'herbi': {'cow', 'horse', 'rabbit'},
'carni': {'lion', 'tiger', 'chetha'},
'reptiles': {'russel viper', 'python','black mamba'}})
print("Before the inversion")
for key in hist:
print(key, hist[key], sep=" : ")
inverse = invert_dict(hist)
print("After the inversion")
for key in inverse:
print(key, inverse[key], sep=" : ")
Step by Step Solution
3.42 Rating (155 Votes )
There are 3 Steps involved in it
Step: 1
f1txtinput file for first program a1 b1 c2 d2 e3 f2 g3 h4 i7 dict1py f openf1txt r sfread the data of the file is in the form of a string which is bei...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