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
Describe the risks associated with their working environment (such as the tools, materials and equipment that they use, spillage
Llana [10]

The risks which are associated with such working environment and not reporting accidental breakages of tools is pollution and damage to the body system of individuals.

<h3>What is Risk?</h3>

This is defined as the possibility of something causing a bad event to occur and is common in workplace when tools are left carelessly which can cause injuries etc.

In the case of a working environment with oil spills, it can lead to slipping of the individuals present in the area and not reporting accidental breakages of tools or equipment could also lead to pollution and different forms of accident which thereby making it the most appropriate choice in this context.

Read more about Risk here brainly.com/question/1224221

#SPJ1

3 0
2 years ago
What are the common approximations made in the analysis of heat exchangers?
Oksanka [162]

Answer: making sure that they are up to date

Explanation:

7 0
3 years ago
Can you tell me important facts about Peggy A. Whitson?
Nat2105 [25]

Answer:

- She’s A Biochemist

- She’s An Accomplished Space Chef

- She holds the record for most time spent in a space station for an American astronaut — of any gender.

- She became the first woman to command the International Space Station twice.

- She made apple pie out of tortillas

6 0
3 years ago
Read 2 more answers
Construct a Mohr circle for the stress element at A in problem 2. Using ruler and compass, draw the Mohr circle to the scale. Dr
serious [3.7K]

Answer:

hello your question is incomplete attached below is the missing diagram to the question and the detailed solution

Answer : principal stresses : 0.82 MPa, -33.492 MPa

              shear stress = 17.157 MPa

              ∅ = 9.09 ≈ 10°

Explanation:

The principal stress ( б1 ) = 0.82 MPa

                                 ( б2 ) = -33.492 MPa

The shear stress = 17.157 MPa

∅ = 9.09 ≈ 10°

attached below is the detailed solution and the Mohr's circle

7 0
3 years ago
If E = 94.2 mJ of energy is transferred when Q = 1.66 C of charge flows through a circuit element, what is the voltage across th
Anni [7]

Answer:

V = 56.8 mV

Explanation:

When a current I flows across a circuit element, if we assume that the dimensions of the circuit are much less than the wavelength of the power source creating this current, there exists a fixed relationship between the power dissipated in the circuit element, the current I and the voltage V across it, as follows:

P = V*I

By definition, power is the rate of change of energy, and current, the rate of change of the charge Q, so we can replace P and I, as follows:

E/t = V*q/t ⇒ E = V*Q

Solving for V:

V = E/Q = 94.2 mJ /1.66 C = 56.8 mV

6 0
3 years ago
Other questions:
  • Which of these is not applicable to tuning for some Overshoot on Start-up? Select one: 1)-No overshoot during normal modulating
    11·1 answer
  • The three areas of knowledge that are required for a product safety engineer are:
    6·1 answer
  • What will occur when the trough of Wave A overlaps the trough of Wave B?
    8·2 answers
  • What is tcp in network?
    13·1 answer
  • Create a document that includes a constructor function named Company in the document head area. Include four properties in the C
    7·1 answer
  • The following median grain size data were obtained during isothermal liquid phase sintering of an 82W-8Mo-8Ni-2Fe alloy. What is
    15·1 answer
  • Many manufacturers currently offer SAE level _________ equipped vehicles
    10·1 answer
  • Technician a says that if a tapered roller bearing is adjusted to loose
    15·1 answer
  • The only function of a flywheel is to cool the engine during operation. <br> True or False?
    5·2 answers
  • The force of impact is
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!