The following program is given:
def invert_dict(d):
inverse = dict()
for key in d:
val = d[key]
if val not in inverse:
inverse[val] = [key]
else:
inverse[val].append(key)
print(inverse)
Days = dict()
Days['Monday'] = 'First Day of the Week'
Days['Tuesday'] = 'Second Day of the Week'
Days['Wednesday'] = 'Third Day of the Week'
Days['Thursday'] = 'Fourth Day of the Week'
Days['Friday'] = 'Fith Day of the Week'
Days['Saturday'] = 'Sixth Day of the Week'
Days['Sunday'] = 'Seventh Day of the Week'
invert_dict(Days)
I have no clue, nor idea how am I supposed to do the following tasks:
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.
I have the 2 files that I need, but I can't figure out how I have to do this.
The code below is as far as I got and I'm getting a 'str' object has no attribute 'read'
error on the as reader
line.
g='file 1 location'
h='file 2 location' # Where to write inverted content of file 1
import ast
import os
def invert_dict(d):
inverse = dict()
for key in d:
val = d[key]
if val not in inverse:
inverse[val] = [key]
else:
inverse[val].append(key)
print(inverse)
Days = dict()
Days['Monday'] = 'First Day of the Week'
Days['Tuesday'] = 'Second Day of the Week'
Days['Wednesday'] = 'Third Day of the Week'
Days['Thursday'] = 'Fourth Day of the Week'
Days['Friday'] = 'Fith Day of the Week'
Days['Saturday'] = 'Sixth Day of the Week'
Days['Sunday'] = 'Seventh Day of the Week'
invert_dict(Days)
with open(g,encode="utf-8") as reader
h=reader.readlines(g)
with open(g,'w',encode="utf-8") as a_writer