4.9 LAB: Sorting TV Shows (dictionaries and lists)
Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since multiple shows could have the same number of seasons).
Sort the dictionary by key (greatest to least) and output the results to a file named output_keys.txt. Separate multiple TV shows associated with the same key with a semicolon (;), ordering by appearance in the input file. Next, sort the dictionary by values (in reverse alphabetical order), and output the results to a file named output_titles.txt.
Ex: If the input is:
file1.txt
and the contents of file1.txt are:
20
Gunsmoke
30
The Simpsons
10
Will & Grace
14
Dallas
20
Law & Order
12
Murder, She Wrote
the file output_keys.txt should contain:
10: Will & Grace
12: Murder, She Wrote
14: Dallas
20: Gunsmoke; Law & Order
30: The Simpsons
and the file output_titles.txt should contain:
Dallas
Gunsmoke
Law & Order
Murder, She Wrote
The Simpsons
Will & Grace
Note: End each output file with a newline, and file1.txt is available to download.
my code so far:
file_name = input()
user_file = open(file_name)
input_list = user_file.readlines()
my_diction = {}
for index in range (len(input_list)):
if index % 2 == 0:
diction_key = input_list[index].strip(‘\n’)
if diction_key not in my_diction:
my_diction[diction_key] = []
else:
my_diction[diction_key].append(input_list[index].strip(‘\n’))
sorted_keys = sorted(my_diction.keys())
output_file = ”
tv_show_list = []
for d_key in sorted_keys:
output_file += d_key.lstrip(‘0’) + ‘: ‘
for tvshow in my_diction[d_key]:
output_file += tvshow + ‘; ‘
tv_show_list.append(tvshow)
output_file = output_file[:-2] + ‘\n’
print(output_file)
file = open(“output_keys.txt”, “w”)
file.write(output_file)
file.close()
tv_show_list.sort()
sorted_list = ”
for tv_show in tv_show_list:
sorted_list += tv_show + ‘\n’
print(sorted_list)
file = open(“output_titles.txt”, “w”)
file.write(sorted_list)
file.close
my output is incorrect:
1:Compare outputkeyboard_arrow_up
0 / 1
10: Will & Grace 12: Murder, She Wrote 14: Dallas 20: Gunsmoke; Law & Order 30: The Simpsons Dallas Gunsmoke Law & Order Murder, She Wrote The Simpsons Will & Grace
Output differs. See highlights below. Special character legend
Input
file1.txt
Your file content
10: Will & Grace 12: Murder, She Wrote 14: Dallas 20: Gunsmoke; Law & Order 30: The Simpsons
Expected file content
30: The Simpsons 20: Gunsmoke; Law & Order 14: Dallas 12: Murder, She Wrote 10: Will & Grace
2:Compare outputkeyboard_arrow_up
0 / 1
10: Will & Grace 12: Murder, She Wrote 14: Dallas 20: Gunsmoke; Law & Order 30: The Simpsons Dallas Gunsmoke Law & Order Murder, She Wrote The Simpsons Will & Grace
Output differs. See highlights below. Special character legend
Input
file1.txt
Your file content
Dallas Gunsmoke Law & Order Murder, She Wrote The Simpsons Will & Grace
Expected file content
Will & Grace The Simpsons Murder, She Wrote Law & Order Gunsmoke Dallas
please help where am I go wrong here?
this is my code that I have written.