Home > Software design >  I have a list and i want to print a specific string from it how can i do that?
I have a list and i want to print a specific string from it how can i do that?

Time:12-12

this is how my data looks like

So far I have done this but this returns the movie name but i want the year 1995 like this in separate list.

moviename=[]
for i in names:
    moviename.append(i.split(' (',1)[0])

CodePudding user response:

One issue with the code you have is that you're getting the first element of the array returned by split, which is the movie title. You want the second argument split()[1].

That being said, this solution won't work very well for a couple of reasons.

  1. You will still have the second parenthesis in the year "1995)"
  2. It won't work if the title itself has parenthesis (e.g. for Shanghai Triad)

If the year is always at the end of each string, you could do something like this.

movie_years = []
for movie in names:
  movie_years.append(movie[-5:-1])

CodePudding user response:

You could use a regular expression.

\(\d*\) will match an opening bracket, following by any number of digit characters (0-9), followed by a closing bracket.

Put only the \d part inside a capturing group to get only that part.

year_regex = r'\((\d )\)'
moviename=[]

for i in names:
    if re.search(year_regex, i):
        moviename.append(re.search(year_regex, i).group(1))

By the way, you can make this all more concise using a list comprehension:

year_regex = r'\((\d )\)'
moviename = [re.search(year_regex, name_and_year).group(1)
             for name_and_year in names
             if re.search(year_regex, name_and_year)]
  • Related