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/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](http://img.homeworklib.com/questions/e979a720-b4b6-11eb-a329-d90ba34ea072.png?x-oss-process=image/resize,w_560)
Output:
![dic - Notepad File Edit Format View Help Animals: [Dog], Birds: [ Peacock? Reptile [Turtlei](http://img.homeworklib.com/questions/e9df4560-b4b6-11eb-9fdd-f115d20e8bbe.png?x-oss-process=image/resize,w_560)
![(file Read, {Reptile: [Turtle], Birds: [Peacock],, Animals [Dog],}) (Dictionary inverted, { [Pea](http://img.homeworklib.com/questions/ea335340-b4b6-11eb-99de-c111d2874e95.png?x-oss-process=image/resize,w_560)
![invert_dict - Notepad File Edit Format View Help [Peacock],:Birds: ITurtle]: Reptile: [Dog], : Animals:](http://img.homeworklib.com/questions/ea92bbc0-b4b6-11eb-81c7-695f2363c545.png?x-oss-process=image/resize,w_560)
Note:
Please give me a positive rating
Thank you:)
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...
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...