Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 拆分编号字符串_Python - Fatal编程技术网

Python 拆分编号字符串

Python 拆分编号字符串,python,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. Eve

我有以下字符串:

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开始的一个连续字符串。一直到“person's existence.\r”,而不是被“is a bad person”和“You pass the test!”!“

使用
re.split

Ex:

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

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. 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
试试这个

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)
输出:

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

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

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. 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')
在每一行上循环

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

第三,我声明了一个空列表和字符串,用于生成我们的最后一个句子,并将每个最后的句子收集到一个列表中

  • .isdigit()
    为True时,我们需要指定最后一句

  • .isdigit()
    为False时,我们需要将字符串附加到 最后一句

    • 现在,我们需要将最后一句添加到新列表中。哪一个是 是在哪里做的?(棘手部分)

    • 我们的
      最后一句
      在再次分配新的 字符串,即
      .isdigit()
      中的字符串为True。我们正在检查,
      最后一句是否有数据。如果是,请附加到
      我们的
      新列表


  • 你已经试过什么了?你能给我们一个你想要达到的输出的例子吗?这个问题没有答案。试试这个正则表达式:
    re.findall('(\d+\..*\n?),s)
    @MPJ567看看如何创建一个。看起来是一个很好的解决方案,我希望你能进一步解释它,我不太明白它为什么有效,我很想知道,这样我就能更好地在将来应用它。我得到它在换行符上拆分,然后检查第一个字符是否是数字,然后我就丢失了。特别是,我无法理解“if t:”在做什么。@MPJ567补充了解释。由于您对学习感兴趣,我为
    new\u list.append(final\u句)
    留下了解释部分,它在
    for loop
    之外为您使用。我想你已经明白了。愉快的学习。您的答案非常接近,我很喜欢学习re.split和zip,但我已经将Shaik的答案标记为答案,因为它保留了数字后的句点。仅供参考,如果您需要数字后的句点,请使用
    (\d+\)
    -->例如:
    data=re.split(r)(\d+\),data)[1: