1

と の位置を取得しようとして<いますが、これはこのようなものに埋め込まれている場合に>現実的です。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 を の 1 番目と 2 番目の位置に<する<tag "XXXX">...</>必要があり、cto と incto を の 2 番目と 1 番目の位置にする必要があり>ます。<tag "XXXX">...</>

センテンス 2 やセンテンス 3 のようなセンテンスに対しても、どのように実行できます<か?><tag "XXXX">...</>

文1の場合、私は単純にこれを行うことができます:

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
4

2 に答える 2

0

私がこれを正しく理解していれば、これはうまくいくはずです(変数を変更しないと仮定してi ,c

cfrom,cto = 0,0
for i,c in enumerate(sentence1):
  if c == "<tag":
    cfrom == i 
  break

for i,c in enumerate(sentence1):
  if c == ">":
    cto == i \\going forward from cfrom
  break

incfrom incto = 0,0
fromtrigger, totrigger = False, False
for i,c in enumerate(sentence1[cto:]):\\after the tag is opened, look for the start of closing tag
  if c == "</":
    incfrom = i
  break
for i,c in enumerate(sentence1[cto:]):
  if c == ">":
    incto = i
  break
于 2013-06-05T15:25:26.990 に答える