Question

Modify your program from Learning Journal Unit 7 to read dictionary items from a file and...

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.

Include the following in your Learning Journal submission:

  • The input file for your original dictionary (with at least six items).
  • The Python program to read from a file, invert the dictionary, and write to a different file.
  • The output file for your inverted dictionary.
  • A description of how you chose to encode the original dictionary and the inverted dictionary in text files.

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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!

Add a comment
Know the answer?
Add Answer to:
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT