Course Hero Logo
Question
Answered step-by-step

7.9 LAB: Sorting TV Shows (dictionaries and lists) Write a program...

7.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 (least to greatest) and output the results to a file named output_keys.txt, separating multiple TV shows associated with the same key with a semicolon (;). 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

Answer & Explanation
Verified Solved by verified expert

ipiscing elit. Nam lacinia pulvinar tortor nec fa

ipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor s

Unlock full access to Course Hero

Explore over 16 million step-by-step answers from our library

Subscribe to view answer
Step-by-step explanation

phprzo0qF.pngphpiawFKm.pngphpRiH6hR.png


sum

molestie consequat, ult

icitur l

sque dapibus efficitur laoreet. Nam

entesque dapibus efficitur

nec facilisis. Pellentesque dapibus effi

at, ultrices ac magna. Fusce

llentesq

amet, consectetur adipiscing elit

ipiscing elit. Nam lacinia pul

dictum vitae odio. Done

icitur laoreet. Nam risu

rem i

ec aliquet. Lorem ip

a molestie

sus ante, dapibus a molestie con

o. Donec aliquet. Lorem ipsum dolor

ng elit. Nam lacinia pulvinar t

Fusce dui lectus, congue vel laoresue vel laoreet ac, dict

congue vel laoreet ac, dictum vitae odio. Donec ali

or nec facilisis. Pellentesque dap

ia pulvinar

or nec facilisis. Pellentes

onec aliquet. Lorem

o. Donec aliquet. Lorem ipsum dolor

et, consectetur adipiscing

s a molestie consequat, ultrices ac

cing elit. N

ipsum dolor

icitur laoreet. N

consectetur adipiscing el

, dictum vitae o

a. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio.

llent

o. Donec aliquet. Lorem ipsum dolor

trices ac magna. Fusce dui lectus, con

ctum vitae odio. Donec aliquet. Lo

ipsum d

ur laoreet. Nam risus ante, dapibus a

sum do

3 Attachments
phpiawFKm.png
png
phpRiH6hR.png
png
phprzo0qF.png
png