IDNLearn.com makes it easy to find accurate answers to your specific questions. Discover thorough and trustworthy answers from our community of knowledgeable professionals, tailored to meet your specific needs.

There is a file of a few Sean Connery movies on the internet. Each entry has the following form:
Name of Movie
Release Date
Running Time (maybe)
Several names of some of the stars
A separator line (except after the last movie)
Your assignment is: for each movie,
1. read in the Name, Release Date, the stars
2. Sort the movies by Movie Name alphabetically,
2. Sort the stars alphabetically, by last name
3. Print the Movie Name, Release Date, Running Time (if there) and Print the list of stars (alphabetically, by last name).
Click here to see the Input File
Some Sample Output:
Darby O'Gill and the Little People
1959
RT 93 minutes
Sean Connery
Walter Fitzgerald
Kieron Moore
Janet Munro
Jimmy O'Dea
Albert Sharp
Estelle Winwood


Sagot :

Answer:

filename = input("Enter file name: ")

movie_container = list()

with open(filename, "r") as movies:

   content = movies.readlines()

   for movie in content:

       holder = movie.split("\t")

       movie_container.append((holder[0], holder[1], holder[2], holder[3]))

movie_container = sorted(movie_container, key= lambda movie: movie[0])

for movie in movie_container:

   movie_stars = movie[3].split(",")

   name_split = [names.split(" ") for names in movie_stars]

   movie_stars.clear()

   name_split = sorted(name_split, key= lambda names: names[1])

   for name in name_split:

       full_name = " ".join(name)

       print( full_name)

       movie_stars.append(full_name)

   movie[3] = movie_stars

print(movie_container)

Explanation:

The python code uses the open function to open and read in the input file from the standard user prompt. The title of the movies and the stars are all sorted alphabetically and display at the output.