finding indices in a sequence of parentheses
Raymond Hettinger
python at rcn.com
Sun May 29 23:16:59 EDT 2005
[Steven Bethard]
>> I have a list of strings that looks something like:
>> lst = ['0', '0', '(*)', 'O', '(*', '*', '(*', '*))', '((*', '*)', '*)']
. . .
>> I want the indices:
>> (2, 2), (4, 7), (6, 7), (8, 9) and (8, 10)
opener_stack = []
for i, elem in enumerate(lst):
for c in elem:
if c == '(':
opener_stack.append(i)
elif c == ')':
print opener_stack.pop(), i
To see something like this in production code, look at
Tools/scripts/texcheck.py
Raymond Hettinger
More information about the Python-list
mailing list