2016-03-07 91 views
1
timeline = ''' 
1961 - Second child, John, is born. 1963 - Is ordained as a Presbyterian minister. Makes his on-camera debut as host of a series of 15-minute episodes for children produced in Toronto. The program is titled "Mister Rogers." 1964 - Returns to Pittsburgh 
    and turns his 15-minute show into the half-hour "Mister Rogers' Neighborhood." 1968 - "Mister Rogers' Neighborhood" debuts on PBS and wins the first of its several Emmy Awards. Rogers is appointed chairman of the Forum on Mass Media and Child Development 
    of the White House Conference on Youth. 1969 - Wins the first of his two George Foster Peabody Awards for television excellence. Fred Rogers readies the opening pitch to start the Pirates' season in 1988. (Post-Gazette archives) 1971 - Forms his production 
    company, Family Communications Inc. 1975 - Ceases production on "Mister Rogers' Neighborhood," which continues to air on PBS in reruns. 1978 - Creates the PBS series "Old Friends, New Friends," focusing on older people. 1979 - Production resumes on 
    "Mister Rogers' Neighborhood." 1981 - Eddie Murphy plays an inner-city children's show host in his "Mister Robinson's Neighborhood" sketch on "Saturday Night Live," which is probably the most famous of the many "Mister Rogers" spoofs. 1984 - The Smithsonian 
    Institution in Washington makes Fred Rogers' trademark sweater part of its permanent collection. Rogers forces the fast-food chain Burger King to remove ads featuring a Mister Rogers lookalike. 1987 - Appears on a children's TV show in the Soviet 
    Union and reciprocates by having a Soviet host appear on his show later in the year. 1989 - "Mister Rogers' Neighborhood of Make Believe" opens as an attraction at Idlewild Park. 1990 - Sues the Ku Klux Klan and forces the organization to stop playing 
    racist telephone recordings featuring imitations of Fred Rogers' voice, speech patterns and theme song. 1991 - Tapes spots for PBS on the eve of the Gulf War to reassure children that they will be all right if hostilities occur. 1996 - TV Guide names 
    Fred Rogers one of the 50 greatest TV stars of all time. Rogers makes his only appearance as someone other than himself when he takes a cameo role as a preacher on the drama series "Dr. Quinn, Medicine Woman," which he says is one of his favorite 
    shows. 1997 - Is honored for lifetime achievement by the National Academy of Television Arts and Sciences (who hand out the Emmy Awards) and by the Television Critics Association. Named Pittsburgher of the Year by Pittsburgh magazine. 1998 - Gets 
    a star on the Hollywood Walk of Fame. The Pittsburgh Children's Museum opens a Mister Rogers exhibit. 1999 - Is inducted into the Television Hall of Fame. 2000 - Unveils a planetarium show featuring "Neighborhood" characters at the Carnegie Science 
    Center; ceases production on "Mister Rogers' Neighborhood." 2001 - Last original episodes of "Mister Rogers' Neighborhood" airs on PBS. 2002 - In July, is presented the Presidential Medal of Freedom, the nation's highest civilian honor. Filmed PBS 
    public service announcements in August encouraging parents to read to their children and giving them advice on how to handle the first anniversary of the Sept. 11, 2001, terrorist attacks. Is named grand marshal of the Tournament of Roses Parade in 
    Pasadena, Calif., with Bill Cosby and Art Linkletter. The theme: "Children's Wishes, Dreams and Imagination." 2003 - Dies of stomach cancer at age 74. 

''' 

我的目标是让这是一个字典,其中键是岁月,值是时间线上的事件。但是,我有点麻烦。如何在某个字符之前拆分字符串?

我想先将时间线分成如下所示的列表:{key,value,key,value},然后将其转换为字典。我想在“ - ”之前分割字符串5或6个字符以完成此操作,但我不知道如何操作。谁能帮我?

编辑: 想出了这个可能的解决方案 -

timeline = timeline.replace("\n","") 
timeline = timeline.replace("\\","") 
timeline = timeline.replace("19","zz19") 
timeline = timeline.replace("20","zz20") 
timeline = timeline.split(" - ") 

所以基本上我结束了这一点:

['zz1961', 'Second child, John, is born. zz1963', 'Is ordained as a Presbyterian minister. Makes his on-camera debut as host of a series of 15-minute episodes for children produced in Toronto. The program is titled "Mister Rogers." zz1964', 'Returns to Pittsburgh and turns his 15-minute show into the half-hour "Mister Rogers\' Neighborhood." zz1968', '"Mister Rogers\' Neighborhood" debuts on PBS and wins the first of its several Emmy Awards. Rogers is appointed chairman of the Forum on Mass Media and Child Development of the White House Conference on Youth. zz1969', "Wins the first of his two George Foster Peabody Awards for television excellence. Fred Rogers readies the opening pitch to start the Pirates' season in zz1988. (Post-Gazette archives) zz1971", 'Forms his production company, Family Communications Inc. zz1975', 'Ceases production on "Mister Rogers\' Neighborhood," which continues to air on PBS in reruns. zz1978', 'Creates the PBS series "Old Friends, New Friends," focusing on older people. zz1979', 'Production resumes on "Mister Rogers\' Neighborhood." zz1981', 'Eddie Murphy plays an inner-city children\'s show host in his "Mister Robinson\'s Neighborhood" sketch on "Saturday Night Live," which is probably the most famous of the many "Mister Rogers" spoofs. zz1984', "The Smithsonian Institution in Washington makes Fred Rogers' trademark sweater part of its permanent collection. Rogers forces the fast-food chain Burger King to remove ads featuring a Mister Rogers lookalike. zz1987", "Appears on a children's TV show in the Soviet Union and reciprocates by having a Soviet host appear on his show later in the year. zz1989", '"Mister Rogers\' Neighborhood of Make Believe" opens as an attraction at Idlewild Park. zz1990', "Sues the Ku Klux Klan and forces the organization to stop playing racist telephone recordings featuring imitations of Fred Rogers' voice, speech patterns and theme song. zz1991", 'Tapes spots for PBS on the eve of the Gulf War to reassure children that they will be all right if hostilities occur. zz1996', 'TV Guide names Fred Rogers one of the 50 greatest TV stars of all time. Rogers makes his only appearance as someone other than himself when he takes a cameo role as a preacher on the drama series "Dr. Quinn, Medicine Woman," which he says is one of his favorite shows. zz1997', 'Is honored for lifetime achievement by the National Academy of Television Arts and Sciences (who hand out the Emmy Awards) and by the Television Critics Association. Named Pittsburgher of the Year by Pittsburgh magazine. zz1998', "Gets a star on the Hollywood Walk of Fame. The Pittsburgh Children's Museum opens a Mister Rogers exhibit. zz1999", 'Is inducted into the Television Hall of Fame. zz2000', 'Unveils a planetarium show featuring "Neighborhood" characters at the Carnegie Science Center; ceases production on "Mister Rogers\' Neighborhood." zz2001', 'Last original episodes of "Mister Rogers\' Neighborhood" airs on PBS. zz2002', 'In July, is presented the Presidential Medal of Freedom, the nation\'s highest civilian honor. Filmed PBS public service announcements in August encouraging parents to read to their children and giving them advice on how to handle the first anniversary of the Sept. 11, zz2001, terrorist attacks. Is named grand marshal of the Tournament of Roses Parade in Pasadena, Calif., with Bill Cosby and Art Linkletter. The theme: "Children\'s Wishes, Dreams and Imagination." zz2003', 'Dies of stomach cancer at age 74.'] 

所以,我想通过“ZZ”拆分此字符串时,唯一的问题是我无法分两次,因为它已经是一个列表。显然,我可以连接列表,但是我现在只会遇到同样的问题。

试着这样做:

for i in timeline: 
    i = i.split("zz") 

但使用正则表达式

+0

你的预期是最终结果。我也很困惑,为什么你要改成列表然后字典。 – Leb

+1

您可以举一个更简单的例子,以及预期的输出吗? –

+0

开始于:时间轴=''' 1961 - 约翰,第二个孩子出生。 1963年 - 被任命为长老会部长。作为在多伦多制作的儿童系列15分钟剧集的主持人,他的相机首次亮相。该节目的标题是“罗杰斯先生”。 ''' 预期结果列表:时间轴= [“1961”,“第二个孩子,约翰,出生。”,“1963”,“被任命为长老会部长。这个节目的标题是“罗杰斯先生”。“”] –

回答

2

我们分了它不工作:(

import re 
stripped_string = timeline.replace('\n', '').strip() # whitespaces go away 
splitted = re.split('(\d{4}) - ', stripped_string) # split to ['', year1, event1, year2, event2, ...] 
lst = splitted[1:] # first empty split goes away 
keys = lst[0::2] # get years 
values = lst[1::2] # get events 
dic = dict(zip(keys, values)) 

你可以使其更加具体又例如,!如果你知道年份为19xx或20xx,那么:

splitted = re.split('((?:19|20)\d\d) - ', stripped_string) 

也可以。

此外,由于您明确指定了keysvalues,因此您可以轻松地完成几件事情,例如剥去values中每个元素的空格。例如:

values = [event.strip() for event in lst[1::2]] 

这里的结果:

>>> print("\n".join("{}: {}".format(k, v) for k, v in dic.items())) 
1989: "Mister Rogers' Neighborhood of Make Believe" opens as an attraction at Idlewild Park. 
1987: Appears on a children's TV show in the Soviet Union and reciprocates by having a Soviet host appear on his show later in the year. 
1984: The Smithsonian Institution in Washington makes Fred Rogers' trademark sweater part of its permanent collection. Rogers forces the fast-food chain Burger King to remove ads featuring a Mister Rogers lookalike. 
1968: "Mister Rogers' Neighborhood" debuts on PBS and wins the first of its several Emmy Awards. Rogers is appointed chairman of the Forum on Mass Media and Child Development of the White House Conference on Youth. 
1969: Wins the first of his two George Foster Peabody Awards for television excellence. Fred Rogers readies the opening pitch to start the Pirates' season in 1988. (Post-Gazette archives) 
1981: Eddie Murphy plays an inner-city children's show host in his "Mister Robinson's Neighborhood" sketch on "Saturday Night Live," which is probably the most famous of the many "Mister Rogers" spoofs. 
1964: Returns to Pittsburgh and turns his 15-minute show into the half-hour "Mister Rogers' Neighborhood." 
1961: Second child, John, is born. 
1963: Is ordained as a Presbyterian minister. Makes his on-camera debut as host of a series of 15-minute episodes for children produced in Toronto. The program is titled "Mister Rogers." 
1991: Tapes spots for PBS on the eve of the Gulf War to reassure children that they will be all right if hostilities occur. 
1990: Sues the Ku Klux Klan and forces the organization to stop playing racist telephone recordings featuring imitations of Fred Rogers' voice, speech patterns and theme song. 
1997: Is honored for lifetime achievement by the National Academy of Television Arts and Sciences (who hand out the Emmy Awards) and by the Television Critics Association. Named Pittsburgher of the Year by Pittsburgh magazine. 
1979: Production resumes on "Mister Rogers' Neighborhood." 
1996: TV Guide names Fred Rogers one of the 50 greatest TV stars of all time. Rogers makes his only appearance as someone other than himself when he takes a cameo role as a preacher on the drama series "Dr. Quinn, Medicine Woman," which he says is one of his favorite shows. 
1999: Is inducted into the Television Hall of Fame. 
1998: Gets a star on the Hollywood Walk of Fame. The Pittsburgh Children's Museum opens a Mister Rogers exhibit. 
1975: Ceases production on "Mister Rogers' Neighborhood," which continues to air on PBS in reruns. 
1978: Creates the PBS series "Old Friends, New Friends," focusing on older people. 
1971: Forms his production company, Family Communications Inc. 
2002: In July, is presented the Presidential Medal of Freedom, the nation's highest civilian honor. Filmed PBS public service announcements in August encouraging parents to read to their children and giving them advice on how to handle the first anniversary of the Sept. 11, 2001, terrorist attacks. Is named grand marshal of the Tournament of Roses Parade in Pasadena, Calif., with Bill Cosby and Art Linkletter. The theme: "Children's Wishes, Dreams and Imagination." 
2003: Dies of stomach cancer at age 74. 
2000: Unveils a planetarium show featuring "Neighborhood" characters at the Carnegie Science Center; ceases production on "Mister Rogers' Neighborhood." 
2001: Last original episodes of "Mister Rogers' Neighborhood" airs on PBS. 
+0

谢谢,我会试试这个! –

2

你可以把字典是这样的:

parts = timeline.replace('\n','').split(" - ") 
d = dict() 
for x in range(len(parts) - 1): 
    d[parts[x][-4:]] = parts[x + 1][:len(parts[x+1]) - 4] 

由于您的分隔符显然是" - "并通过这样做,你会设置的项目如:

'1961' 
'blablablablabla. 1963' 
'blablablablabla. 1968' 
'blablablablabla. 1969' 
'blablablablabla' 

然后,您只需将文本中的最后4个字符与除最后四个字符以外的下一个文本配对。因此,简单的分割和切片将适用于您。

以此来显示结果:

for a in d: 
    print(a + " " + d[a]) 

这是我得到:

1975 Ceases production on "Mister Rogers' Neighborhood," which continues to air on PBS in reruns. 
1968 "Mister Rogers' Neighborhood" debuts on PBS and wins the first of its several Emmy Awards. Rogers is appointed chairman of the Forum on Mass Media and Child Development of the White House Conference on Youth. 
2001 Last original episodes of "Mister Rogers' Neighborhood" airs on PBS. 
1990 Sues the Ku Klux Klan and forces the organization to stop playing racist telephone recordings featuring imitations of Fred Rogers' voice, speech patterns and theme song. 
2002 In July, is presented the Presidential Medal of Freedom, the nation's highest civilian honor. Filmed PBS public service announcements in August encouraging parents to read to their children and giving them advice on how to handle the first anniversary of the Sept. 11, 2001, terrorist attacks. Is named grand marshal of the Tournament of Roses Parade in Pasadena, Calif., with Bill Cosby and Art Linkletter. The theme: "Children's Wishes, Dreams and Imagination." 
1989 "Mister Rogers' Neighborhood of Make Believe" opens as an attraction at Idlewild Park. 
1996 TV Guide names Fred Rogers one of the 50 greatest TV stars of all time. Rogers makes his only appearance as someone other than himself when he takes a cameo role as a preacher on the drama series "Dr. Quinn, Medicine Woman," which he says is one of his favorite shows. 
1987 Appears on a children's TV show in the Soviet Union and reciprocates by having a Soviet host appear on his show later in the year. 
1963 Is ordained as a Presbyterian minister. Makes his on-camera debut as host of a series of 15-minute episodes for children produced in Toronto. The program is titled "Mister Rogers." 
1984 The Smithsonian Institution in Washington makes Fred Rogers' trademark sweater part of its permanent collection. Rogers forces the fast-food chain Burger King to remove ads featuring a Mister Rogers lookalike. 
1971 Forms his production company, Family Communications Inc. 
1991 Tapes spots for PBS on the eve of the Gulf War to reassure children that they will be all right if hostilities occur. 
1997 Is honored for lifetime achievement by the National Academy of Television Arts and Sciences (who hand out the Emmy Awards) and by the Television Critics Association. Named Pittsburgher of the Year by Pittsburgh magazine. 
1999 Is inducted into the Television Hall of Fame. 
1964 Returns to Pittsburgh and turns his 15-minute show into the half-hour "Mister Rogers' Neighborhood." 
2000 Unveils a planetarium show featuring "Neighborhood" characters at the Carnegie Science Center; ceases production on "Mister Rogers' Neighborhood." 
1981 Eddie Murphy plays an inner-city children's show host in his "Mister Robinson's Neighborhood" sketch on "Saturday Night Live," which is probably the most famous of the many "Mister Rogers" spoofs. 
1978 Creates the PBS series "Old Friends, New Friends," focusing on older people. 
1969 Wins the first of his two George Foster Peabody Awards for television excellence. Fred Rogers readies the opening pitch to start the Pirates' season in 1988. (Post-Gazette archives) 
1961 Second child, John, is born. 
2003 Dies of stomach cancer at age 
1998 Gets a star on the Hollywood Walk of Fame. The Pittsburgh Children's Museum opens a Mister Rogers exhibit. 
1979 Production resumes on "Mister Rogers' Neighborhood." 

注意,字典是无序的。如果您需要订购,请使用OrderedDict

有序字典一样容易,字典做过这样的:

import collections 
parts = timeline.replace('\n','').split(" - ") 
od = collections.OrderedDict() 
for x in range(len(parts) - 1): 
    od[parts[x][-4:]] = parts[x + 1][:len(parts[x+1]) - 4] 

并使用此:

for a in od: 
    print(a + " " + od[a]) 

结果将下令:

1961 Second child, John, is born. 
1963 Is ordained as a Presbyterian minister. Makes his on-camera debut as host of a series of 15-minute episodes for children produced in Toronto. The program is titled "Mister Rogers." 
1964 Returns to Pittsburgh and turns his 15-minute show into the half-hour "Mister Rogers' Neighborhood." 
1968 "Mister Rogers' Neighborhood" debuts on PBS and wins the first of its several Emmy Awards. Rogers is appointed chairman of the Forum on Mass Media and Child Development of the White House Conference on Youth. 
1969 Wins the first of his two George Foster Peabody Awards for television excellence. Fred Rogers readies the opening pitch to start the Pirates' season in 1988. (Post-Gazette archives) 
1971 Forms his production company, Family Communications Inc. 
1975 Ceases production on "Mister Rogers' Neighborhood," which continues to air on PBS in reruns. 
1978 Creates the PBS series "Old Friends, New Friends," focusing on older people. 
1979 Production resumes on "Mister Rogers' Neighborhood." 
1981 Eddie Murphy plays an inner-city children's show host in his "Mister Robinson's Neighborhood" sketch on "Saturday Night Live," which is probably the most famous of the many "Mister Rogers" spoofs. 
1984 The Smithsonian Institution in Washington makes Fred Rogers' trademark sweater part of its permanent collection. Rogers forces the fast-food chain Burger King to remove ads featuring a Mister Rogers lookalike. 
1987 Appears on a children's TV show in the Soviet Union and reciprocates by having a Soviet host appear on his show later in the year. 
1989 "Mister Rogers' Neighborhood of Make Believe" opens as an attraction at Idlewild Park. 
1990 Sues the Ku Klux Klan and forces the organization to stop playing racist telephone recordings featuring imitations of Fred Rogers' voice, speech patterns and theme song. 
1991 Tapes spots for PBS on the eve of the Gulf War to reassure children that they will be all right if hostilities occur. 
1996 TV Guide names Fred Rogers one of the 50 greatest TV stars of all time. Rogers makes his only appearance as someone other than himself when he takes a cameo role as a preacher on the drama series "Dr. Quinn, Medicine Woman," which he says is one of his favorite shows. 
1997 Is honored for lifetime achievement by the National Academy of Television Arts and Sciences (who hand out the Emmy Awards) and by the Television Critics Association. Named Pittsburgher of the Year by Pittsburgh magazine. 
1998 Gets a star on the Hollywood Walk of Fame. The Pittsburgh Children's Museum opens a Mister Rogers exhibit. 
1999 Is inducted into the Television Hall of Fame. 
2000 Unveils a planetarium show featuring "Neighborhood" characters at the Carnegie Science Center; ceases production on "Mister Rogers' Neighborhood." 
2001 Last original episodes of "Mister Rogers' Neighborhood" airs on PBS. 
2002 In July, is presented the Presidential Medal of Freedom, the nation's highest civilian honor. Filmed PBS public service announcements in August encouraging parents to read to their children and giving them advice on how to handle the first anniversary of the Sept. 11, 2001, terrorist attacks. Is named grand marshal of the Tournament of Roses Parade in Pasadena, Calif., with Bill Cosby and Art Linkletter. The theme: "Children's Wishes, Dreams and Imagination." 
2003 Dies of stomach cancer at age 
+0

谢谢,我会试试这个! –

+0

感谢这工作得很好唯一的是它在所有撇号前添加了一个“\”。 (例如George's => George's) –

+0

这是怎么回事? –