In Python, I'm attempting to make a fast way of replacing bogus parentheses in a string with periods
-
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 . For example: s1 = '((.))' s2 = '((.)' s3 = '(.))' Fixing these would give: rs1 = '((.))' rs2 = '.(.)' rs3 = '(.).' Right now, I'm using a stack to get all the bogus () then using a list comprehension to replace them. Given that fold is one of the s* strings above, here's my code: hashFoldStack = [] makePeriods = set() for hashFoldIndex, hashFoldCharacter in enumerate(fold): if hashFoldCharacter == '(': hashFoldStack.append(hashFoldIndex) elif hashFoldCharacter == ')': if len(hashFoldStack) > 0: hashFoldStack.pop() else: makePeriods.update([hashFoldIndex]) if len(hashFoldStack) > 0: for leftover in hashFoldStack: makePeriods.update(leftover) hashFold = ''.join(['.' if hashIndex in makePeriods\ else hashCharacter\ for hashIndex, hashCharacter\ in enumerate(fold)]) 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?
-
Answer:
I think the code is pretty fast. Let's make it work now :-) if len(hashFoldStack) > 0: for leftover in hashFoldStack: makePeriods.update(leftover) should be just: makePeriods.update(hashFoldStack) I was searching net a bit and http://www.skymind.com/~ocrow/python_string/ I found a great article about the topic. It mentions that the string construction by join and list comprehension is one of the fastest ways. If you want to optimize further, use some way of profiling as the comments suggest. You can wrap the code in for loop that runs lots of times and then launch the script like this $ python -m cProfile script.py. By the way, there could be a way to make it faster by constructing hashFoldList in the loop together with hashFoldStack. At the end, it would be just: hashFold = ''.join(hashFoldList). Anyway, an interesting question.
user1005909 at Stack Overflow Visit the source
Other answers
Does it only need to work on segments of two parentheses at most? Here's a simple answer: def pblocks(pblocks): whole = [] for i in pblocks: L, R = i.split('.') p = '.' if len(L) == len(R): part = [L, R] elif len(L) < len(R): part = [L, ').'] else: part = ['.(', R] whole.append(part) return ['.'.join(i) for i in whole] For more elaborate examples, you'd have to give me a moment, but I think you could continue deleting segments until len(L) == len(R), but I'm not sure what you'd want done with multiple leftover parens.
Droogans
Related Q & A:
- What's a fast way to lose weight?Best solution by Yahoo! Answers
- How to make a fast mini sailboat?Best solution by Yahoo! Answers
- What's a fast way to lose 15 pounds?Best solution by Yahoo! Answers
- I'm trying to write a song?Best solution by Yahoo! Answers
- I'm trying to get a meeting with a diplomat to counsel me on a career in international affairs?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.