inserting bracketings into a string

Steven Bethard steven.bethard at gmail.com
Wed Nov 17 12:40:03 EST 2004


Eddie Corns wrote:
> def insert_bracketings (txt, spans):
>     text = list(txt)
>     for tg,start,end in spans:
>         text[start] = '[%s %s'%(tg,text[start])
>         text[end-1] = '%s]'%text[end-1]
>     return ''.join(text)
> 
> print insert_bracketings('abcde fgh ijklmnop qrstu vw xyz',[('A', 0, 9), ('B', 6, 9), ('C', 25, 31)])
> 
> Might not give what you expect if two spans start at the same place but you
> haven't defined that.

If two spans start in the same place, I need them both to appear, but 
the order is not important, so I believe your code here should work 
fine.  Very nice, thank you!

Steve



More information about the Python-list mailing list