Question

In: Computer Science

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

------------------------------------------------------------------------------------------------------------------

The Unit 7 program:

Dr_Appointments = {'Mom':[2, 'April', 2020], 'Brother':[6, 'July', 2020], 'Sister':[6, 'January', 2021], }

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

print('Dr. Appointments:', Dr_Appointments)

print('Inverted Dr. Appointments:')
Invert_Appointments = invert_dict(Dr_Appointments)
print(Invert_Appointments)

This is the expanded dictionary: the Unit 7 plus three more:

Mom: 2, April, 2020,
Brother: 6, July, 2020,
Sister: 6, January, 2021,
Aunt: 2, December, 2021,
Uncle: 6, November, 2021,
Niece: 2, December, 2020

Solutions

Expert Solution

ANSWER:

I have provided the properly commented and indented code so you can easily copy the code as well as check for correct indentation.
I have provided the output image of the code so you can easily cross-check for the correct output of the code.
Have a nice and healthy day!!

CODE

# inverting dict
def invert_dict(d):
    # defining empty dict
    inverse = dict()
    # looping through each key value of dict
    for key in d:
        val = d[key]
        # converting val list to tuple to use it as key
        val = tuple(val)
        if val not in inverse:
            inverse[val] = [key]
        else:
            inverse[val].append(key)
        
    # returning inverse 
    return inverse


# first reading file and storing content in as dictonary in variable
# opening file
file = open("f1.txt",'r')

# now reading content of file
content = file.read()

# loading content in dictonary format using eval funtion of python
data = eval(content)

# inverting data
inv_data = invert_dict(data)

# converting inv_data to string
string_inv = str(inv_data)

# storing string_inv to file
file_write = open("f2.txt",'w')

# writing to file_write
file_write.write(string_inv)

# file object to close
file_write.close()

INPUT IMAGE (f1.txt)

OUTPUT IMAGE

f2.txt


Related Solutions

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...
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...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named recur that is defined as follows: if i ≤ 0 or j ≤ 0, recur(i, j) = 0. if i = j, recur(i, j) = i. if i > j, recur(i, j) = j. In all other cases, recur(i, j) = 2 · recur(i − 1, j) + recur(j − 1, i) Add...
At this level your program must read in (from a file) at least four albums and...
At this level your program must read in (from a file) at least four albums and up to 15 tracks for each album. The information read from the file should include: Number of Albums Album title Artist Artwork file name (place your artwork in an /images folder under the main folder where you run the program) The number of tracks The title of each track The file location of each track At this level user interaction must be entirely through...
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than...
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than the user. You should use appropriate exception handling when reading from the file. The text file that you will read is provided. -------------------- Modify GaveDriver2.java -------------------- -----GameDriver2.java ----- import java.io.File; import java.util.ArrayList; import java.util.Scanner; /** * Class: GameDriver * * This class provides the main method for Homework 2 which reads in a * series of Wizards, Soldier and Civilian information from the user...
Modify the Assignment program from Module 1 to read a user's first and last name read...
Modify the Assignment program from Module 1 to read a user's first and last name read three integer scores, calculate the total and average determine the letter grade based on the following criteria - Average 90-100 grade is A, 80-89.99 grade is B, 70-79.99 grade is C, all others F (use a nested if) Output formatted results consisting of the full name, the three scores, the total, average (to 2 decimal places), and the letter grade go to step 1...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards of each type of card are in the file. credit_cards.txt contains the following data: John Smith, Discover Helen Jones, Visa Jerry Jones, Master Card Julio Jones, Diners Club Fred Jones, Diners Club Anthony Rendon, Platinum Visa Juan Soto, Platinum Visa George Jones, American Express Brandon Allen, Visa Henry Beureguard, Visa Allen Jackson, Master Card Faith Hill, Platinum Visa David Smith, Master Card Samual Jackson,...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT