Re: Python replace multiple strings (m*n) combination

2017-02-25 Thread Cem Karan
Another possibility is to form a suffix array (https://en.wikipedia.org/wiki/Suffix_array#Applications) as an index for the string, and then search for patterns within the suffix array. The basic idea is that you index the string you're searching over once, and then look for patterns within

Re: Python replace multiple strings (m*n) combination

2017-02-25 Thread INADA Naoki
If you can use third party library, I think you can use Aho-Corasick algorithm. https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm https://pypi.python.org/pypi/pyahocorasick/ On Sat, Feb 25, 2017 at 3:54 AM, wrote: > I have a task to search for multiple patterns

Re: Python replace multiple strings (m*n) combination

2017-02-24 Thread kar6308
On Friday, February 24, 2017 at 2:41:58 PM UTC-8, Erik wrote: > On 24/02/17 22:18, kar wrote: > > Thanks, what is the idea behind storing the keys and values in a list, I > > assume looking up for a value in a map is faster getting the value from the > > list. > > What do you not understand?

Re: Python replace multiple strings (m*n) combination

2017-02-24 Thread Erik
On 24/02/17 22:18, kar wrote: Thanks, what is the idea behind storing the keys and values in a list, I assume looking up for a value in a map is faster getting the value from the list. What do you not understand? MRAB's extensive comments explain what is being done and why. In summary, the

Re: Python replace multiple strings (m*n) combination

2017-02-24 Thread MRAB
On 2017-02-24 22:18, kar wrote: On Friday, February 24, 2017 at 11:48:22 AM UTC-8, MRAB wrote: On 2017-02-24 18:54, kar6...@gmail.com wrote: > I have a task to search for multiple patterns in incoming string and replace with matched patterns, I'm storing all pattern as keys in dict and

Re: Python replace multiple strings (m*n) combination

2017-02-24 Thread kar
On Friday, February 24, 2017 at 11:48:22 AM UTC-8, MRAB wrote: > On 2017-02-24 18:54, kar6...@gmail.com wrote: > > I have a task to search for multiple patterns in incoming string and > > replace with matched patterns, I'm storing all pattern as keys in dict and > > replacements as values, I'm

Re: Python replace multiple strings (m*n) combination

2017-02-24 Thread Erik
On 24/02/17 18:54, kar6...@gmail.com wrote: for example is the search string is not a variable we can say re.search(r"\$%^search_text", "replace_text", "some_text") but when I read from the dict where shd I place the "r" keyword, unfortunately putting inside key doesnt work "r key" like

Re: Python replace multiple strings (m*n) combination

2017-02-24 Thread MRAB
On 2017-02-24 18:54, kar6...@gmail.com wrote: I have a task to search for multiple patterns in incoming string and replace with matched patterns, I'm storing all pattern as keys in dict and replacements as values, I'm using regex for compiling all the pattern and using the sub method on

Python replace multiple strings (m*n) combination

2017-02-24 Thread kar6308
I have a task to search for multiple patterns in incoming string and replace with matched patterns, I'm storing all pattern as keys in dict and replacements as values, I'm using regex for compiling all the pattern and using the sub method on pattern object for replacement. But the problem I