当它们嵌入到这样的东西中时,<
我试图获得它们的位置。>
tag
<tag "510270">calculate</>
我有这样的句子:
sentence = "After six weeks and seventeen tentative approaches the only serious
tender came from Daniel. He had offered a paltry #2 a week for the one-time
woodman's home, sane enough in this, at least, to <tag "510270">calculate</>
safety to the nearest new penny piece. "
sentence2 = "After six weeks and seventeen tentative approaches the only serious
tender came from Daniel. He had offered a paltry #2 a week for the one-time
woodman's < home, sane enough in this, at least, to <tag "510270">calculate</>
safety to the nearest new penny > piece. "
sentence3 = "After six weeks and seventeen tentative approaches the only serious
tender came from Daniel. He had offered a paltry #2 a week for the one-time
woodman's > home, sane enough in this, at least, to <tag "510270">calculate</>
safety to the nearest new penny < piece. "
我需要 cfrom 和 incfrom 成为第一个和第二个<
的位置,我需要 cto 和 incto<tag "XXXX">...</>
成为第二个和第一个>
的位置<tag "XXXX">...</>
<
对于句子 2和>
句子 3之类的句子,我怎么能做到这一点<tag "XXXX">...</>
?
对于 sentence1,我可以简单地这样做:
cfrom,cto = 0,0
for i,c in enumerate(sentence1):
if c == "<":
cfrom == i
break
for i,c in enumerate(sentence1.reverse):
if c == ">":
cto == len(sentence)-i
break
incfrom incto = 0,0
fromtrigger, totrigger = False, False
for i,c in enumerate(sentence1[cfrom:]):
if c == ">":
incfrom = cfrom+i
break
for i,c in enumerate(sentence1[incfrom:cto]):
if c == "<":
incto = i
break