
Concept explainers
PYTHON:
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.
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
MY CODE:
seasons_and_shows = input()
file_input = open(seasons_and_shows, 'r')
data = file_input.readlines()
titles_and_seasons = {}
list_of_titles = []
for i in range(0, len(data)):
data[i] = data[i].strip()
for i in range(0, len(data), 2):
list_of_titles.append(data[i + 1])
if data[i] in titles_and_seasons:
titles_and_seasons[data[i]] = titles_and_seasons[data[i]] + '; ' + data[i + 1]
else:
titles_and_seasons[data[i]] = data[i + 1]
with open('output_keys.txt', 'w') as output_keys:
for item in sorted(titles_and_seasons.items()):
output_keys.write(str(item[0]) + ': ' + item[1] + '\n')
with open('output_titles.txt', 'w') as output_titles:
list_of_titles.sort()
for each in list_of_titles:
output_titles.write(each + '\n')
I am getting no output and need to define a function that sorts by the values in the dictionary instead of the keys since the keys are not unique.

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 5 images

- Please answer the ques in python with showing the codesarrow_forwardWrite a python program that creates a dictionary containing the U.S. states as keys, and their capitals as values. The program should then randomly quiz the user by displaying the name of a state and asking the user to enter that state's capital. The program should keep a count of the number of correct and incorrect responses. This program has really been giving me trouble. Any help is greatly appreciated. Thanks so much!arrow_forwardWrite a program that creates a dictionary containing Canadian provinces' abbreviations as the keys (e.g., 'on') and their long format as the values (e.g., 'Ontario'). Use the internet to get a list of provinces and their abbreviations. The program should then randomly quiz the user by displaying the abbreviation of the province and asking the user to enter the extended format for the abbreviation. The program should keep count of the number of correct and incorrect responses.arrow_forward
- Write a python program that reads the contents of a text file. The program should create a dictionary in which the keys are the individual words found in the file and the values are the number of times each word appears. For example, if the word "the" appears 128 times, the dictionary would contain an element with 'the' as the key and 128 as the value. The program should either display the frequency of each word or create a second file containing a list of each word and it's frequency. This program has really been giving me trouble. Any help is great appreciated. Thanks so much!arrow_forwardPYTHON Complete the function below, which takes two arguments: data: a list of tweets search_words: a list of search phrases The function should, for each tweet in data, check whether that tweet uses any of the words in the list search_words. If it does, we keep the tweet. If it does not, we ignore the tweet. data = ['ZOOM earnings for Q1 are up 5%', 'Subscriptions at ZOOM have risen to all-time highs, boosting sales', "Got a new Mazda, ZOOM ZOOM Y'ALL!", 'I hate getting up at 8am FOR A STUPID ZOOM MEETING', 'ZOOM execs hint at a decline in earnings following a capital expansion program'] Hint: Consider the example_function below. It takes a list of numbers in numbers and keeps only those that appear in search_numbers. def example_function(numbers, search_numbers): keep = [] for number in numbers: if number in search_numbers(): keep.append(number) return keep def search_words(data, search_words):arrow_forwardIn Python languagearrow_forward
- This code is full of errors, what are 4 of them and how I would fix them?arrow_forwardIn python: student_dict is a dictionary with students' name and pedometer reading pairs. A new student is read from input and added into student_dict. For each student in student_dict, output the student's name, followed by "'s pedometer reading: ", and the student's pedometer reading. Then, assign average_value with the average of all the pedometer readings in student_dict..arrow_forwardIn Python, create a telephone dictionary where the user is asked whether they wish to add, remove,modify, search for a telephone number (given the name), or exit. Use functions for each one ofthe four different operations. Use an infinite looparrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





