6.10 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 (least to greatest) 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 (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: There is a newline at the end of each output file, and file1.txt is available to download. MY CODE: filename=input('') dict_info={} with open(filename, 'r') as infile: lines=infile.readlines() for i in range(0, len(lines) -1, 2): if lines [i].strip()==' ':continue count=int(lines[i].strip()) name=lines[i+1].strip() if count in dict_info.keys(): names=dict_info.get(count) names.append(name) names.sort() else: dict_info[count]=[name] print(count, name) if dict_info is None: print('{}'.format(file)) outFile1 = 'output_keys.txt' outFile2 = 'output_titles.txt' with open(outFile1, 'w+') as output: for i in sorted(dict_info.keys()): output.write('{}: {}\n'.format(i,'; '.join(dict_info.get(i)))) print('{}: {}\n'.format(i,'; '.join(dict_info.get(i)))) titleNames=[] for i in dict_info.values(): titleNames.extend(i) with open(outFile2, 'w+') as output: for i in sorted(titleNames): output.write('{}\n'.format(i)) print(i) Output is not correct.
6.10 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 (least to greatest) 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 (alphabetical order), and output the results to a file named output_titles.txt.
Ex: If the input is:
file1.txtand the contents of file1.txt are:
20 Gunsmoke 30 The Simpsons 10 Will & Grace 14 Dallas 20 Law & Order 12 Murder, She Wrotethe file output_keys.txt should contain:
10: Will & Grace 12: Murder, She Wrote 14: Dallas 20: Gunsmoke; Law & Order 30: The Simpsonsand the file output_titles.txt should contain:
Dallas Gunsmoke Law & Order Murder, She Wrote The Simpsons Will & GraceNote: There is a newline at the end of each output file, and file1.txt is available to download.
MY CODE:
filename=input('')
dict_info={}
with open(filename, 'r') as infile:
lines=infile.readlines()
for i in range(0, len(lines) -1, 2):
if lines [i].strip()==' ':continue
count=int(lines[i].strip())
name=lines[i+1].strip()
if count in dict_info.keys():
names=dict_info.get(count)
names.append(name)
names.sort()
else:
dict_info[count]=[name]
print(count, name)
if dict_info is None:
print('{}'.format(file))
outFile1 = 'output_keys.txt'
outFile2 = 'output_titles.txt'
with open(outFile1, 'w+') as output:
for i in sorted(dict_info.keys()):
output.write('{}: {}\n'.format(i,'; '.join(dict_info.get(i))))
print('{}: {}\n'.format(i,'; '.join(dict_info.get(i))))
titleNames=[]
for i in dict_info.values():
titleNames.extend(i)
with open(outFile2, 'w+') as output:
for i in sorted(titleNames):
output.write('{}\n'.format(i))
print(i)
Output is not correct.
![Output differs. See highlights below. Special character legend
Input
file2.txt
7: Lux Video Theatre; Medium; Rules of Engagement
8: Barney Miller; Castle; Mama
10: Friends; Modern Family; Smallville; Will & Grace
11: Cheers; The Jeffersons
Your file content
12: Murder, She Wrote; NYPD Blue
14: Bonanza; Dallas
15: ER
20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit
30: The Simpsons
7: Rules of Engagement; Medium; Lux Video Theatre
8: Mama; Barney Miller; Castle
10: Will & Grace; Smallville; Modern Family; Friends
11: Cheers; The Jeffersons
Expected file content
12: Murder, She Wrote; NYPD Blue
14: Dallas; Bonanza
15: ER
20: Gunsmoke; Law & Order; Law & Order: Special Victims Unit
30: The Simpsons](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fd6ded41a-9f92-4a9d-b437-015644cf6784%2F69312f5c-6b62-4e5f-b25a-e8bafcf6f235%2F0yl7z0u_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)