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:
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.
Include the following in your Learning Journal submission:
Here is my code:
def invert_dict(d):
inverse = dict()
for key in d:
val = d[key]
for item in val:
inverse[item]=key;
return inverse
#hist is creating a dictionary to identify the list of animal families and its type.
hist = dict({'Herbi': {'cow', 'horse', 'rabbit'},
'Carnivore': {'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("\nAfter the inversion")
for key in inverse:
print(key,inverse[key], sep=" : ")
==
Output:
Before the inversion
Herbi : {'rabbit', 'horse', 'cow'}
Carnivore : {'chetha', 'tiger', 'lion'}
Reptiles : {'python', 'black mamba', 'russel viper'}
After the inversion
python : Reptiles
black mamba : Reptiles
russel viper : Reptiles
We identify the list of animals according to their kind. Also inverted dictionary is helpful to identify whether the particular is herbivore, carnivore or reptile family. But here this seems the inverted dictionary is not very helpful because it consumes more memory.
The required answer is bold.. What I need for the answer is above unbolded texts.

def readDict(fileName):
result = {}
for line in open(fileName):
l = line.strip()
key, values = l.split(':', 1)
values = [x.strip() for x in values.split(',')]
result[key.strip()] = values
return result
def writeDict(fileName, d):
f = open(fileName, 'w')
for (key, values) in d.items():
f.write(key + ':' + values + '\n')
f.close()
def invert_dict(d):
inverse = dict()
for key in d:
val = d[key]
for item in val:
inverse[item]=key
return inverse
#hist is creating a dictionary to identify the list of animal families and its type.
hist = readDict('data.txt')
print("Before the inversion")
for key in hist:
print(key,hist[key], sep=" : ")
inverse = invert_dict(hist)
print("\nAfter the inversion")
for key in inverse:
print(key,inverse[key], sep=" : ")
# write the final dict to file
writeDict('inverted_dict.txt', inverse)
data.txt:
Herbi : horse,rabbit,cow
Carnivore : chetha,lion,tiger
Reptiles : python,russel viper,black mamba
Please upvote, as i have given the exact answer as asked in
question. Still in case of any issues in code, let me know in
comments. Thanks!
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and...
Python 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...
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...
Hello,
Similar questions like this have been previously answered, but
this question is different because it has only one part while
similar questions have 2 parts and their solutions is a combination
of the 2 parts. I have not been able to find an absolute solution
for this part.
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 need to decide on the...
Write a French/English dictionary lookup program. Read a list of pairs of English and French words from a file specified by the user. English/French words should be exact matches (don't try to find partial matches). Use the supplied EnglishFrenchDictionary.java class as your main class. Fill in the missing code in the DictionaryTable.java class to read the input file and perform the searches. Add code to the DictionaryTable read() method to: read pairs of lines (English word is on the first...