首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多个分隔符拆分,同时将分隔符保留为字典键

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

Stack Overflow用户
提问于 2017-04-25 21:51:54
回答 1查看 521关注 0票数 0

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

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

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

代码语言:javascript
复制
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)

输出

代码语言:javascript
复制
['',
 'N-Methyl-D-as ..., 
 'In this study, ...', 
 'All 32 subunit ...', 
 ...]

欲望输出

代码语言:javascript
复制
{'BACKGROUND\n': 'N-Methyl-D-as ...',
 'METHODS\n': 'In this study, ...', 
 ...}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-25 21:57:42

代码语言:javascript
复制
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']

values = re.split('|'.join(delimiters), abstract)
values.pop(0)  # remove the initial empty string
keys = re.findall('|'.join(delimiters), abstract)
output = dict(zip(keys, values))

print(output)
# {'BACKGROUND\n': 'N-Methyl-D-aspartate (NMDA) receptors are glutamate-activated ion channels that are assembled from NR1 and NR2 subunits. \nThese receptors are highly enriched in brain neurons and are considered to be an important target for the acute and chronic effects of ethanol. \nNR2 subunits (A-D) arise from separate genes and are expressed in a developmental and brain region-specific manner. \nThe NR1 subunit has 8 isoforms that are generated by alternative splicing of a single gene. \nThe heteromeric subunit makeup of the NMDA receptor determines the pharmacological and biophysical properties of the receptor and provides for functional receptor heterogeneity. \nAlthough 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\n\n', 'METHODS\n': 'In 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\n\n', 'RESULTS\n': 'All 32 subunit combinations tested gave reproducible glutamate-activated currents and all receptors were inhibited to some degree by 100 mM ethanol. \nThe 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\n\n', 'CONCLUSIONS\n': 'These results suggest that regional, developmental, or compensatory changes in the expression of NR1 splice variants may significantly affect ethanol inhibition of NMDA receptors.\n'}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43621596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档