使用Python

时间:2017-06-05 13:46:01

标签: python string dictionary replace

我正在处理一个我必须预处理的数据集。我想用它们唯一的ID替换所有出现的事件(由开始和结束索引给出)。

给出一串文字,如:

s = "The hypotensive effect of 100 mg/kg alpha-methyldopa was also partially reversed by naloxone. Naloxone alone did not affect either blood pressure or heart rate. In brain membranes from spontaneously hypertensive rats clonidine, 10(-8) to 10(-5) M, did not influence stereoselective binding of [3H]-naloxone (8 nM), and naloxone, 10(-8) to 10(-4) M, did not influence naloxone-suppressible binding of [3H]-dihydroergocryptine (1 nM)."

和一系列词典,如:

[

' D006973':[{'长度':' 12',             ' offset':' 199',             '文字':['高血压'],             '输入疾病'},

' D008750':[{'长度':' 16',             ' offset':' 36',             ' text':[' alpha-methyldopa'],             '键入':'化学'}],

' D007022':[{' length':' 11',             ' offset':' 4',             ' text':[' hypotensive'],             '输入疾病'},

' D009270':[{' length':' 8',             ' offset':' 84',             '文字':['纳洛酮'],             '输入':'化学品'},            {'长度':' 8',             ' offset':' 94',             '文字':['纳洛酮'],             '输入':'化学品'},            {'长度':' 13',             '抵消':' 293',             ' text':[" [3H] -naloxone"],             '输入':'化学品'}]

我想将偏移量给出的所有出现次数替换为各自的ID。因此,对于最后一个字典,我希望列表中的所有值都被' D009270'替换。

示例1:对于带有键' D006973'的第一个词典,我想要替换出现在索引199并且长度为12的高血压'其中' D006973&#39 ;.

示例2:对于带有键' D009270'的最后一个字典,我想从索引替换子字符串(由元组给出)

[(84, 92), (94, 102), (293, 306)]
  1. 在最后一句中,纳洛酮与" 纳洛酮可抑制"同时存在,但我不想替换它。所以我不能简单地使用str.replace()

  2. 我使用其唯一ID将字符串从起始索引替换为结束索引(例如:199到211,高血压')。但这会扰乱其他尚未被替换的补偿。实体。 我可以使用填充来替换要替换的文本(' D006973')小于字符串('高血压')。但是当要重新调整的文本的大小更大时,它将会失败。

1 个答案:

答案 0 :(得分:0)

您可以将字符串格式化程序与占位符字符一起使用:

from operator import itemgetter

s = "The hypotensive effect of 100 mg/kg alpha-methyldopa was also partially reversed by naloxone. Naloxone alone did not affect either blood pressure or heart rate. In brain membranes from spontaneously hypertensive rats clonidine, 10(-8) to 10(-5) M, did not influence stereoselective binding of [3H]-naloxone (8 nM), and naloxone, 10(-8) to 10(-4) M, did not influence naloxone-suppressible binding of [3H]-dihydroergocryptine (1 nM)."

dictionary={
'D006973': [{'length': '12', 'offset': '199', 'text': ['hypertensive'], 'type': 'Disease'}],
'D008750': [{'length': '16', 'offset': '36', 'text': ['alpha-methyldopa'], 'type': 'Chemical'}],
'D007022': [{'length': '11', 'offset': '4', 'text': ['hypotensive'], 'type': 'Disease'}],
'D009270': [{'length': '8', 'offset': '84', 'text': ['naloxone'], 'type': 'Chemical'}, {'length': '8', 'offset': '94', 'text': ['Naloxone'], 'type': 'Chemical'}, {'length': '13', 'offset': '293', 'text': ["[3H]-naloxone"], 'type': 'Chemical'}]
}

index_list=[]
for key in dictionary:
    for dic in dictionary[key]:
        o=int(dic['offset'])
        index_tuple=o , o+int(dic['length']),key
        index_list.append(index_tuple)

index_list.sort(key=itemgetter(0))
format_list=[]
lt=list(s)
for i,j in enumerate(index_list):
    si=j[0]
    ei=j[1]
    lt[si:ei]=list("{}") + ["@"]*((ei-si)-2)
    format_list.append(j[2])

text = "".join(lt)
text = text.replace("@","")
text = text.format(*format_list)

结果:'The D007022 effect of 100 mg/kg D008750 was also partially reversed by D009270. D009270 alone did not affect either blood pressure or heart rate. In brain membranes from spontaneously D006973 rats clonidine, 10(-8) to 10(-5) M, did not influence stereoselective binding of D009270 (8 nM), and naloxone, 10(-8) to 10(-4) M, did not influence naloxone-suppressible binding of [3H]-dihydroergocryptine (1 nM).'