Part 1 Modify your program from Learning Journal Unit 7 to read dictionary items from a file
Question:
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=" : ")