1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Andreyy89
3 years ago
6

9.21 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:
Engineering
1 answer:
natka813 [3]3 years ago
8 0

The following code or the program will be used

<u>Explanation:</u>

def readFile(filename):

   dict = {}

   with open(filename, 'r') as infile:

       lines = infile.readlines()

       for index in range(0, len(lines) - 1, 2):

           if lines[index].strip()=='':continue

           count = int(lines[index].strip())

           name = lines[index + 1].strip()

           if count in dict.keys():

               name_list = dict.get(count)

               name_list.append(name)

               name_list.sort()

           else:

               dict[count] = [name]

           print(count,name)

   return dict

def output_keys(dict, filename):

   with open(filename,'w+') as outfile:

       for key in sorted(dict.keys()):

           outfile.write('{}: {}\n'.format(key,';'.join(dict.get(key))))

           print('{}: {}\n'.format(key,';'.join(dict.get(key))))  

def output_titles(dict, filename):

   titles = []

   for title in dict.values():

       titles.extend(title)

   with open(filename,'w+') as outfile:

       for title in sorted(titles):

           outfile.write('{}\n'.format(title))

           print(title)

def main():

   filename = input('Enter input file name: ')

   dict = readFile(filename)

   if dict is None:

       print('Error: Invalid file name provided: {}'.format(filename))

       return

   print(dict)

   output_filename_1 ='output_keys.txt'

   output_filename_2 ='output_titles.txt'

   output_keys(dict,output_filename_1)

   output_titles(dict,output_filename_2)  

main()

You might be interested in
Weight limit for a pallet
hammer [34]

Answer:

standard pallet can withstand 4,600lbs

Explanation:

6 0
3 years ago
Read 2 more answers
So in my settings i set it to send notifications too my email so ik when smtn happens but it doesn't even send stuff too it.....
attashe74 [19]

Answer:

Did you make sure you did it and your notifications are on

3 0
3 years ago
Que es resistencia ?
IRINA_888 [86]
It is when you don’t give up and you stand for your rights
5 0
3 years ago
Which type of elevated stand does not need a tree?
SashulF [63]

Answer:

Elevated stands place the hunter above ground level. They can be tree stands placed in or against trees, or freestanding structures. They have become increasingly popular in recent years with both firearm and bow hunters.

Explanation:

4 0
2 years ago
Koch traded Machine 1 for Machine 2 when the fair market value of both machines was $60,000. Koch originally purchased Machine 1
Mariana [72]

Answer:

Koch's adjusted basis in machine 2 after the exchange is $60,000

Explanation:

given data

fair market value = $60,000

originally purchased Machine 1 = $76,900

Machine 1 adjusted basis = $40,950

Machine 2 seller purchase = $64,050

Machine 2 adjusted basis = $55,950

solution

As he exchanged machine for another at $60,000

and this exchanged in fair market

so adjusted basis =  $50,000

Adjusted basis is the price of the item that affects the factors that are considered price. These factors usually include taxes, depreciation value, and other costs of acquiring and maintaining a given item. Adjusted basis is important so the right amount to sell

Adjusted basis increases when a person deducts expenses from factor taxes and operating statements

so Koch's adjusted basis in machine 2 after the exchange is $60,000

3 0
3 years ago
Other questions:
  • 1. Create a class called Name that represents a person's name. The class should have fields named firstName representing the per
    8·2 answers
  • An equal-tangent sag vertical curve connects a 1% and 3% initial and final grades, respectively, and is designed for 70 mph. The
    12·1 answer
  • For some transformation having kinetics that obey the Avrami equation , the parameter n is known to have a value of 1.1. If, aft
    6·1 answer
  • The grade is a measure of quality and it captures concentration levels (i.e., how pure a certain fraction is). If grade captures
    13·1 answer
  • the frequencies 10, 12, 23 and 45 Hz. (a) What is the minimum sampling rate required to avoid aliasing? (b) If you sample at 40
    13·1 answer
  • Which one of the following is not an economic want?
    6·1 answer
  • In a hydraulic system, a 100.-newton force is applied to a small piston with an area of 0.0020 m2. What pressure, in pascals, wi
    13·1 answer
  • Describe in your own words the three strengthening mechanisms
    7·1 answer
  • Quelles sont les types de carburant utilisés en aviation
    15·1 answer
  • If an internally piloted DCV does not shift, you should use a gauge to _____. A.check the pilot line pressure b. check the inlet
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!