inserting bracketings into a string

Steven Bethard steven.bethard at gmail.com
Tue Nov 16 18:38:14 EST 2004


I'm trying to insert some bracketings in a string based on a set of
labels and associated start and end indices.  For example, I'd like to
do something like:

>>> text = 'abcde fgh ijklmnop qrstu vw xyz'
>>> spans = [('A', 0, 9), ('B', 6, 9), ('C', 25, 31)]
>>> insert_bracketings(text, spans)
'[A abcde [B fgh]] ijklmnop qrstu [C vw xyz]'

My current implementation looks like:

>>> def insert_bracketings(text, spans):
... 	starts = [start for _, start, _ in spans]
... 	ends = [end for _, _, end in spans]
... 	indices = sorted(set(starts + ends))
... 	splits = [(text[start:end], start, end)
... 	          for start, end in zip([None] + indices, indices + [None])]
... 	start_map, end_map = {}, {}
... 	for label, start, end in spans:
... 		start_map.setdefault(start, []).append('[%s ' % label)
... 		end_map.setdefault(end, []).append(']')
... 	result = []	
... 	for string, start, end in splits:
... 		if start in start_map:
... 			result.extend(start_map[start])
... 		result.append(string)
... 		if end in end_map:
... 			result.extend(end_map[end])
... 	return ''.join(result)
... 

but it seems like there ought to be an easier way.  Can anyone help me?

Thanks in advance,

Steve
-- 
When you're being strangled, everything you do is anaerobic exercise!
        --- Adam Olshefsky



More information about the Python-list mailing list