2

I have this string and want to turn it into two arrays, one has the film title and the other one has the year. Their positions in the array need to correspond with each other. Is there a way to do this?

films = ("""Endless Love (1981), Top Gun (1986), The Color of Money (1986), Rain Man (1988),
Born on the Fourth of July (1989), Interview with the Vampire: The Vampire Chronicles (1994),
Mission: Impossible (1996), Jerry Maguire (1996), The Matrix (1999), Mission: Impossible II (2000),
Vanilla Sky (2001), Cocktail (1988), A Few Good Men (1992), The Firm (1993), Eyes Wide Shut (1999),
Magnolia (1999), Minority Report (2002), Austin Powers in Goldmember (2002), Days of Thunder (1990),
The Powers of Matthew Star (1982), Cold Mountain (2003), The Talented Mr. Ripley (1999),
 War of the Worlds (2005), The Oprah Winfrey Show (1986), Far and Away (1992), Taps (1981),
 The Last Samurai (2003), Valkyrie (2008), Jack Reacher (2012), Edge of Tomorrow (2014),
 Enemy of the State (1998), Mission: Impossible III (2006), Crimson Tide (1995), Reign Over Me (2007),
 Batman Forever (1995), Batman Begins (2005), The Simpsons (1989), The Simpsons: Brother from the Same Planet (1993),
 The Simpsons: When You Dish Upon a Star (1998), End of Days (1999), House of D (2004), The Indian Runner (1991),
  Harry & Son (1984), Mission: Impossible - Ghost Protocol (2011), Aladdin (1992), Pacific Rim (2013),
  Oblivion (2013), Knight and Day (2010),
""")

4 Answers 4

3

First split the input string on comma to generate a list, then use comprehensions to get the title and year as separate lists.

films_list = re.split(r',\s*', films)
titles = [re.split(r'\s*(?=\(\d+\))', x)[0] for x in films_list]
years = [re.split(r'\s*(?=\(\d+\))', x)[1] for x in films_list]
Sign up to request clarification or add additional context in comments.

3 Comments

Hey, re isn't defined so the code keeps producing an error. what would be the problem?
You need to import re. Also, you need to ignore the final element of films_list due to the comma at the end of the string
@HarrisonCox Please check this demo. I did a cleanup on your films string to make it a single line. Other than this, my answer should be working for you. You need import re at the top of your script as my answer is using regular expressions.
0

Answer of Tim is well enough. I will try to write an alternative someone who would like to solve the problem without using regex.

a = films.split(",")
years = []
for i in a:
    years.append(i[i.find("(")+1:i.find(")")])

Same approach can be applied for titles.

Comments

0

You can do something like this (without any kind of import or extra module needed, or regex complexity):

delimeter = ", "
movies_with_year = pfilms.split(delimeter)
movies = []
years = []
for movie_with_year in movies_with_year:
    movie = movie_with_year[:-6]
    year = movie_with_year[-6:].replace("(","").replace(")","")
    movies.append(movie)
    years.append(year)

This script will result in something like this:

movies : ['Endless Love ', ...]

years : ['1981', ...]

Comments

0

You shuold clear all "new line" (|n) and use try/except to pass over the last elemet issue.

films = ("""Endless Love (1981), Top Gun (1986), The Color of Money (1986), Rain Man (1988),
Born on the Fourth of July (1989), Interview with the Vampire: The Vampire Chronicles (1994),
Mission: Impossible (1996), Jerry Maguire (1996), The Matrix (1999), Mission: Impossible II (2000),
Vanilla Sky (2001), Cocktail (1988), A Few Good Men (1992), The Firm (1993), Eyes Wide Shut (1999),
Magnolia (1999), Minority Report (2002), Austin Powers in Goldmember (2002), Days of Thunder (1990),
The Powers of Matthew Star (1982), Cold Mountain (2003), The Talented Mr. Ripley (1999),
 War of the Worlds (2005), The Oprah Winfrey Show (1986), Far and Away (1992), Taps (1981),
 The Last Samurai (2003), Valkyrie (2008), Jack Reacher (2012), Edge of Tomorrow (2014),
 Enemy of the State (1998), Mission: Impossible III (2006), Crimson Tide (1995), Reign Over Me (2007),
 Batman Forever (1995), Batman Begins (2005), The Simpsons (1989), The Simpsons: Brother from the Same Planet (1993),
 The Simpsons: When You Dish Upon a Star (1998), End of Days (1999), House of D (2004), The Indian Runner (1991),
  Harry & Son (1984), Mission: Impossible - Ghost Protocol (2011), Aladdin (1992), Pacific Rim (2013),
  Oblivion (2013), Knight and Day (2010),
""")

movies = []
years = []
for item in films.replace("\n", "").split("),"):
      try:
        movies.append(item.split(" (")[0])
        years.append(item.split(" (")[-1])
      except:
        ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.