question archive 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

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

Subject:Computer SciencePrice:2.87 Bought7

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?

Include the following in your Learning Journal submission:

  • The input file for your original dictionary (with at least six items).
  • The Python program for Part 1.
  • The output file for your inverted dictionary, which is also the input file for Part 2.
  • The Python program for Part 2.
  • The output file for your twice-inverted dictionary.
  • A description of any differences between your program for Part 1 and your program for Part 2.
  • A description of any differences between the original input file and the final twice-inverted output file.

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:

Input File - dict_in.txt

A:Apple
B:Ball
C:Cat
D:Dog
E:Egg
F:Fish
G:Game
H:Horse
I:India
J:Jug
K:Kite
L:Lemon
M:Monkey
N:Nike
O:Owl
P:Pet
Q:Queue
R:Race
S:Sudden
T:Tale
U:Umbrella
V:Venice
W:Wealth
X:Xylophone
Y:Yacht
Z:Zebra

create_dict.py

f = open("dict_in.txt", "r") #opening the input file in read mode
dict = {} #creating an empty dictionary
for i in f.readlines(): #readin line by line from the file
temp=i.split(":") #spliting the items by ":" and appending them to an array
#(if there is some other separator used instead of ":" then
# that should be put inside split() function)
# the first element in the array is the key and second
# element is the value
dict[temp[0]]=temp[1].strip("\n") #adding the key value pair to the dictionary
## the second element will actually have a "\n" with it
## so the value string needs to be stripped of it
f.close() ##closing the file
##print(dict)
inv_dict = {v: k for k, v in dict.items()} #inverts the dictionary
f1=open("dict_out1.txt","w+") #creating output file
for key,value in inv_dict.items(): #iterating over the inverted dictionary
#print(key,value)
f1.write(str(key)+":"+str(value)+"\n") #writing the key,value pair to the
## output file
f1.close() #closing the file

Output File - dict_out1.txt

Apple:A
Ball:B
Cat:C
Dog:D
Egg:E
Fish:F
Game:G
Horse:H
India:I
Jug:J
Kite:K
Lemon:L
Monkey:M
Nike:N
Owl:O
Pet:P
Queue:Q
Race:R
Sudden:S
Tale:T
Umbrella:U
Venice:V
Wealth:W
Xylophone:X
Yacht:Y
Zebra:Z

create_dict2.py

f = open("dict_out1.txt", "r") #opening the input file in read mode
dict = {} #creating an empty dictionary
for i in f.readlines(): #readin line by line from the file
temp=i.split(":") #spliting the items by ":" and appending them to an array
#(if there is some other separator used instead of ":" then
# that should be put inside split() function)
# the first element in the array is the key and second
# element is the value
dict[temp[0]]=temp[1].strip("\n") #adding the key value pair to the dictionary
## the second element will actually have a "\n" with it
## so the value string needs to be stripped of it
f.close() #closing the file
##print(dict)
inv_dict = {v: k for k, v in dict.items()} #inverts the dictionary
f1=open("dict_out2.txt","w+") #creating output file
for key,value in inv_dict.items(): #iterating over the inverted dictionary
#print(key,value)
f1.write(str(key)+":"+str(value)+"\n") #writing the key,value pair to the
## output file
f1.close() #closing the file

dict_out2.txt

A:Apple
B:Ball
C:Cat
D:Dog
E:Egg
F:Fish
G:Game
H:Horse
I:India
J:Jug
K:Kite
L:Lemon
M:Monkey
N:Nike
O:Owl
P:Pet
Q:Queue
R:Race
S:Sudden
T:Tale
U:Umbrella
V:Venice
W:Wealth
X:Xylophone
Y:Yacht
Z:Zebra

The only difference between program for part 1 and part 2 is  input files are different.

And the output file from program 2 as same as the input file from program 1.