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.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images