Python 使用多个分隔符拆分,同时将分隔符保留为字典键

Python 使用多个分隔符拆分,同时将分隔符保留为字典键,python,regex,dictionary,split,Python,Regex,Dictionary,Split,我想使用给定的多个分隔符拆分文本。但是,我仍然希望在文本前面保留分隔符作为字典的键,而不是返回纯列表 我尝试按顺序使用分隔符列表,但它会生成列表列表,因此我尝试使用正则表达式(re)。但是在re之前,我无法在拆分后跟踪我的分隔符。我想知道是否有一种方法可以在保留分隔符作为键的同时使用分隔符拆分字符串 下面是我当前使用re的解决方案,它给出了一个输出列表 import re abstract = """ BACKGROUND\nN-Methyl-D-aspartate (NMDA) recept

我想使用给定的多个分隔符拆分文本。但是,我仍然希望在文本前面保留分隔符作为字典的键,而不是返回纯列表

我尝试按顺序使用分隔符列表,但它会生成列表列表,因此我尝试使用正则表达式(
re
)。但是在
re
之前,我无法在拆分后跟踪我的
分隔符。我想知道是否有一种方法可以在保留分隔符作为键的同时使用分隔符拆分字符串

下面是我当前使用
re
的解决方案,它给出了一个输出列表

import re

abstract = """
BACKGROUND\nN-Methyl-D-aspartate (NMDA) receptors are glutamate-activated ion channels that are assembled from NR1 and NR2 subunits. 
These receptors are highly enriched in brain neurons and are considered to be an important target for the acute and chronic effects of ethanol. 
NR2 subunits (A-D) arise from separate genes and are expressed in a developmental and brain region-specific manner. 
The NR1 subunit has 8 isoforms that are generated by alternative splicing of a single gene. 
The heteromeric subunit makeup of the NMDA receptor determines the pharmacological and biophysical properties of the receptor and provides for functional receptor heterogeneity. 
Although results from previous studies suggest that NR2 subunits affect the ethanol sensitivity of NMDA receptors, the role of the NR1 subunit and its multiple splice variants is less well known.
\n\n\nMETHODS\nIn this study, all 8 NR1 splice variants were individually coexpressed with each NR2 subunit in human embryonic kidney 293 (HEK293) cells and tested for inhibition by ethanol using patch-clamp electrophysiology.
\n\n\nRESULTS\nAll 32 subunit combinations tested gave reproducible glutamate-activated currents and all receptors were inhibited to some degree by 100 mM ethanol. 
The sensitivity of individual receptors to ethanol was affected by the specific NR1 splice variant expressed with receptors containing the NR1-3 and NR1-4 subunits among the least inhibited by ethanol.
\n\n\nCONCLUSIONS\nThese results suggest that regional, developmental, or compensatory changes in the expression of NR1 splice variants may significantly affect ethanol inhibition of NMDA receptors.
"""

delimiters = ['BACKGROUND\n', 'CONCLUSIONS\n', 'OBJECTIVES\n',
              'METHODS\n', 'OBJECTIVE\n', 'RESULTS\n']

sections = re.split('|'.join(delimiters), abstract)
输出

['',
 'N-Methyl-D-as ..., 
 'In this study, ...', 
 'All 32 subunit ...', 
 ...]
{'BACKGROUND\n': 'N-Methyl-D-as ...',
 'METHODS\n': 'In this study, ...', 
 ...}
渴望输出

['',
 'N-Methyl-D-as ..., 
 'In this study, ...', 
 'All 32 subunit ...', 
 ...]
{'BACKGROUND\n': 'N-Methyl-D-as ...',
 'METHODS\n': 'In this study, ...', 
 ...}