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 following:

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.

My Learning Journal Unit 7:

fav_artist = {
    # artist name : [music genre, age, net-worth]
    "Jay Z": ["rapper", 50, "$ 1 billion"],
    "Beyoncé": ["singer", 38, "$ 470 million"],
    "Nicki Minaj": ["rapper", 37, "$ 85 million"],
    "Drake": ["rapper", 33, "$150 million"],
    "Kanye West": ["rapper", 42, "$1.3 billion"],
    "Usher": ["singer", 41, "$180 million"],
}


def invert_dict(d):             # Modified function
    inverse = dict()
    for key in d:
        val = d[key]
        for item in val:
            if item not in inverse:
                inverse[item] = [key]
            else:
                inverse[item].append(key)
    return inverse


inverted_artist = invert_dict(fav_artist)
print("original dictionary:")
print(fav_artist)
print("inverted dictionary:")
print(inverted_artist)

output:

C:\Users\Goddess\PycharmProjects\UoPeople\venv\Scripts\python.exe C:/Users/Goddess/PycharmProjects/UoPeople/School.py
original dictionary:
{'Jay Z': ['rapper', 50, '$ 1 billion'], 'Beyoncé': ['singer', 38, '$ 470 million'], 'Nicki Minaj': ['rapper', 37, '$ 85 million'], 'Drake': ['rapper', 33, '$150 million'], 'Kanye West': ['rapper', 42, '$1.3 billion'], 'Usher': ['singer', 41, '$180 million']}
inverted dictionary:
{'rapper': ['Jay Z', 'Nicki Minaj', 'Drake', 'Kanye West'], 50: ['Jay Z'], '$ 1 billion': ['Jay Z'], 'singer': ['Beyoncé', 'Usher'], 38: ['Beyoncé'], '$ 470 million': ['Beyoncé'], 37: ['Nicki Minaj'], '$ 85 million': ['Nicki Minaj'], 33: ['Drake'], '$150 million': ['Drake'], 42: ['Kanye West'], '$1.3 billion': ['Kanye West'], 41: ['Usher'], '$180 million': ['Usher']}

Process finished with exit code 0


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

/copyable code

def invert_Dictionary(x):

    inverse=dict()

    for key1 in x.keys():

        list_items = x.get(key1)

        inverse[list_items]=key1

    return inverse

def readfile(file_name):

    x=dict()

    with open(file_name,'r') as infile:

        for line1 in infile.readlines():

            line1=line1.strip().split(':')

            x[line1[0]]=line1[1]

    print('file Read',x)

    return x

def writefile(invert_d,file_name):

    with open(file_name,'w+') as wtfile:

        for key1,value1 in invert_d.items():

            wtfile.write(str(key1)+':'+str(value1)+'\n')

def main():

    x = readfile('dic.txt')

    invt_d = invert_Dictionary(x)

    print('Dictionary inverted',invt_d)

    writefile(invt_d,'inverted_dic.txt')

main()

Screenshot:

def invert_Dictionary (x): inverse=dict() for keyi in x.keys (): list_items = x.get (key1) inverse[list_items] =key1 return i

Output:

dic - Notepad File Edit Format View Help Animals: [Dog], Birds: [ Peacock? Reptile [Turtlei

(file Read, {Reptile: [Turtle], Birds: [Peacock],, Animals [Dog],}) (Dictionary inverted, { [Pea

invert_dict - Notepad File Edit Format View Help [Peacock],:Birds: ITurtle]: Reptile: [Dog], : Animals:

Note:

Please give me a positive rating

Thank you:)

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 write the inverted dictionary to a file. You will need to decide on the following:
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
  • Python Modify your program from Learning Journal Unit 7 to read dictionary items from a file...

    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...

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
ADVERTISEMENT