LoslegenKostenlos loslegen

Splitting by a pattern

You are given the movies list where each element stores a movie name, its release date, and the director (e.g. "The Godfather, 1972, Francis Ford Coppola").

Let's practice some splitting with the help of regular expressions. Your task is to retrieve from each element of the list its name and the director. For example, if the element is "The Godfather, 1972, Francis Ford Coppola", the result would be:

['The Godfather', 'Francis Ford Coppola']

Eventually, this result should be modified to represent a single string, e.g.

"The Godfather, Francis Ford Coppola"

Diese Übung ist Teil des Kurses

Practicing Coding Interview Questions in Python

Kurs anzeigen

Anleitung zur Übung

  • Compile a regular expression that splits strings in movies into a movie name and its director.
  • Retrieve a movie name and its director using re.split().
  • Create a new string with a movie name and its director separated by ,.
  • Append the resulting string to movies_without_year.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Compile a regular expression
pattern = re.compile(____)

movies_without_year = []
for movie in movies:
    # Retrieve a movie name and its director
    split_result = re.split(____)
    # Create a new string with a movie name and its director
    movie_and_director = ', '.____
    # Append the resulting string to movies_without_year
    movies_without_year.____
    
for movie in movies_without_year:
    print(movie)
Code bearbeiten und ausführen