分割编号的字符串

时间:2019-07-03 17:18:30

标签: python

我有以下字符串:

1. the main cause of [explosions].
2. any thing [dreaded] that your "teachers" say is "good" for you. soon after, you explode for no reason.
3. what scientists do to make stuff explode.
4. when a sheet of paper explodes into [flames].
2. Everything that is [put in] [front] of you during any given [day].
3. slang for [testosterone]..[steroid] [hormones].
4. A means of evaluating another person's character through a series of [unannounced], [inconspicuous] examinations. Developed by Eddie, it serves to define which people are desirable company and which should not be associated with. As a general rule, a test-passer is a good person while a test-failer is a bad person.
Passing the test results in the joyous [proclamation] that "You pass the test!"
Failure of a test is announced simply by saying "You failed the test." This phrase is often used to show general disdain for a person's existence.
5. It is terrible.

我想根据\ n拆分此字符串,但在数字4之类的情况下,只能在最后一个\ n字符上进行拆分。我想将第一个数字和下一个数字之间的所有内容捕获为结果列表中的单个字符串。

使用所需的输出列表进行编辑

['1. the main cause of [explosions].\r', 
'2. any thing [dreaded] that your "teachers" say is "good" for you. soon after, you explode for no reason.\r', 
'3. what scientists do to make stuff explode.\r', 
'4. when a sheet of paper explodes into [flames].', 
'2. Everything that is [put in] [front] of you during any given [day]. ', 
'3. slang for [testosterone]..[steroid] [hormones].', 
"4. A means of evaluating another person's character through a series of [unannounced], [inconspicuous] examinations. Developed by Eddie, it serves to define which people are desirable company and which should not be associated with. As a general rule, a test-passer is a good person while a test-failer is a bad person. Passing the test results in the joyous [proclamation] that "You pass the test!" Failure of a test is announced simply by saying "You failed the test." This phrase is often used to show general disdain for a person\'s existence.\r”, 
'5. To [check] if something coresponds the promised result [or what] [effect] does it have at all.', '6. A [process] for [testing] [things]',
'7. What you [take in] school to [determine] if you pass or [fail] in life.', 
'8. The word all students fear.', "A piece of paper that might [screw up] someones life if they don't write anything on it.", 'Something that makes most students wish to die on the weeks before they go into the room and take the test.', 'Something that makes most students have [teamwork] spirit.', 'A paper that teachers love to surprise, [scare] and threat their students with.', 
'9. A [process] of finding out whether something works or not.\r', 
"2. An [oral] or written [exam] to find out one's ability in one or more subjects.", 
'10. To try someone or [go up] against another by [getting on] ones nerves or [manhood].', 
'']

请注意,字符串以“ 4”开头。是从4一直到“人的存在。\ r”的一个连续字符串,而不是在“是一个坏人”和“您通过测试!”之后被\ n分隔。

2 个答案:

答案 0 :(得分:1)

使用re.split

例如:

data = """1. the main cause of [explosions].
2. any thing [dreaded] that your "teachers" say is "good" for you. soon after, you explode for no reason.
3. what scientists do to make stuff explode.
4. when a sheet of paper explodes into [flames].
2. Everything that is [put in] [front] of you during any given [day].
3. slang for [testosterone]..[steroid] [hormones].
4. A means of evaluating another person's character through a series of [unannounced], [inconspicuous] examinations. Developed by Eddie, it serves to define which people are desirable company and which should not be associated with. As a general rule, a test-passer is a good person while a test-failer is a bad person.
Passing the test results in the joyous [proclamation] that "You pass the test!"
Failure of a test is announced simply by saying "You failed the test." This phrase is often used to show general disdain for a person's existence.
5. It is terrible."""

data =re.split(r"(\d+)\.", data)[1:]

result = [n+m for n,m in zip(data[0::2], data[1::2])]
print(result)

输出:

['1 the main cause of [explosions].\n',
 '2 any thing [dreaded] that your "teachers" say is "good" for you. soon '
 'after, you explode for no reason.\n',
 '3 what scientists do to make stuff explode.\n',
 '4 when a sheet of paper explodes into [flames].\n',
 '2 Everything that is [put in] [front] of you during any given [day].\n',
 '3 slang for [testosterone]..[steroid] [hormones].\n',
 "4 A means of evaluating another person's character through a series of "
 '[unannounced], [inconspicuous] examinations. Developed by Eddie, it serves '
 'to define which people are desirable company and which should not be '
 'associated with. As a general rule, a test-passer is a good person while a '
 'test-failer is a bad person.\n'
 'Passing the test results in the joyous [proclamation] that "You pass the '
 'test!"\n'
 'Failure of a test is announced simply by saying "You failed the test." This '
 "phrase is often used to show general disdain for a person's existence.\n",
 '5 It is terrible.']

答案 1 :(得分:1)

尝试一下

data = '''1. the main cause of [explosions].
2. any thing [dreaded] that your "teachers" say is "good" for you. soon after, you explode for no reason.
3. what scientists do to make stuff explode.
4. when a sheet of paper explodes into [flames].
2. Everything that is [put in] [front] of you during any given [day].
3. slang for [testosterone]..[steroid] [hormones].
4. A means of evaluating another person's character through a series of [unannounced], [inconspicuous] examinations. Developed by Eddie, it serves to define which people are desirable company and which should not be associated with. As a general rule, a test-passer is a good person while a test-failer is a bad person.
Passing the test results in the joyous [proclamation] that "You pass the test!"
Failure of a test is announced simply by saying "You failed the test." This phrase is often used to show general disdain for a person's existence.
5. It is terrible.'''

new_list = []
final_sentence = ""

for line in data.split('\n'):
    if line.strip()[0].isdigit():
        if final_sentence:
            new_list.append(final_sentence)
        final_sentence  = line
    else:
        final_sentence +=' '+line

new_list.append(final_sentence)
print(new_list)

输出:

['1. the main cause of [explosions].', 
'2. any thing [dreaded] that your "teachers" say is "good" for you. soon after, you explode for no reason.', 
'3. what scientists do to make stuff explode.', 
'4. when a sheet of paper explodes into [flames].', 
'2. Everything that is [put in] [front] of you during any given [day].', 
'3. slang for [testosterone]..[steroid] [hormones].', 
'4. A means of evaluating another person\'s character through a series of [unannounced], [inconspicuous] examinations. Developed by Eddie, it serves to define which people are desirable company and which should not be associated with. As a general rule, a test-passer is a good person while a test-failer is a bad person. Passing the test results in the joyous [proclamation] that "You pass the test!" Failure of a test is announced simply by saying "You failed the test." This phrase is often used to show general disdain for a person\'s existence.',
'5. It is terrible.']

验证:

>>> len(new_list)
8

说明:

首先,使用.split('\n')遍历每一行。

第二,检查第一个元素是否为数字。

第三,我声明了一个空列表和一个字符串,用于制作我们的最后一句话并将每个final_sentence收集到一个列表中。

  1. .isdigit()为True时,我们需要分配给final_sentence。

  2. .isdigit()为False时,我们需要将字符串附加到 final_sentence。

    • 现在,我们需要将final_sentence附加到new_list。哪一个是 正确的地方吗? (棘手的部分)

    • 我们的final_sentence完全是在再次分配新的{ 字符串,即.isdigit()中的字符串为True。我们正在检查 final_sentence是否有数据。如果是,请附加到 我们的new_list