In Python, I'm attempting to make a fast way of replacing bogus parentheses in a string with periods
<p>Let's say that I have a string with only '(', ')', and '.' and I want to replace all the () that don't match with corresponding () with a .</p>

<p>For example:</p>

<pre><code>s1 = '((.))'
s2 = '((.)'
s3 = '(.))'
</code></pre>

<p>Fixing these would give:</p>

<pre><code>rs1 = '((.))'
rs2 = '.(.)'
rs3 = '(.).'
</code></pre>

<p>Right now, I'm using a stack to get all the bogus () then using a list comprehension to replace them.</p>

<p>Given that fold is one of the s* strings above, here's my code:</p>

<pre><code>hashFoldStack = []
makePeriods = set()
for hashFoldIndex, hashFoldCharacter in enumerate(fold):
    if hashFoldCharacter == '(':
        hashFoldStack.append(hashFoldIndex)
    elif hashFoldCharacter == ')':
        if len(hashFoldStack) &gt; 0:
            hashFoldStack.pop()
        else:
            makePeriods.update([hashFoldIndex])
if len(hashFoldStack) &gt; 0:
    for leftover in hashFoldStack:
        makePeriods.update(leftover)

hashFold = ''.join(['.' if hashIndex in makePeriods\
                        else hashCharacter\
                        for hashIndex, hashCharacter\
                        in enumerate(fold)])
</code></pre>

<p>Question 1: For the first part where I'm creating the makePeriods stack, is there a faster way to do this?
Question 2: Is a list comprehension going to be faster than a lambda or some function from the re module?</p>
