简体   繁体   English

"如何在 python 中将此字符串转换为 2 个数组?"

[英]How do i turn this string into 2 arrays in python?

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),
""")

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]

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(")")])

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)

You shuold clear all "new line" (|n) and use try/except to pass over the last elemet issue.您应该清除所有“新行”(|n)并使用 try/except 来跳过最后一个元素问题。

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:
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM