[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 -- http://mail.python.org/mailman/listinfo/python-list