Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Replacing large number of substrings

Reply
Thread Tools

Replacing large number of substrings

 
 
Will McGugan
Guest
Posts: n/a
 
      09-04-2005
Hi,

Is there a simple way of replacing a large number of substrings in a
string? I was hoping that str.replace could take a dictionary and use it
to replace the occurrences of the keys with the dict values, but that
doesnt seem to be the case.

To clarify, something along these lines..

>>> dict_replace( "a b c", dict(a="x", b="y") )

"x y c"


Regards,

Will McGugan
--
http://www.kelpiesoft.com
 
Reply With Quote
 
 
 
 
tiissa
Guest
Posts: n/a
 
      09-04-2005
Will McGugan wrote:
> Hi,
>
> Is there a simple way of replacing a large number of substrings in a
> string? I was hoping that str.replace could take a dictionary and use it
> to replace the occurrences of the keys with the dict values, but that
> doesnt seem to be the case.


You can look at the re.sub [1] and try:

d={'a':'x', 'b':'y'}

def repl(match):
return d.get(match.group(0), '')

print re.sub("(a|b)", repl, "a b c")



> >>> dict_replace( "a b c", dict(a="x", b="y") )

> "x y c"


Above, I gave the pattern myself but you can try to have it generated
from the keys:


def dict_replace(s, d):
pattern = '(%s)'%'|'.join(d.keys())
def repl(match):
return d.get(match.group(0), '')
return re.sub(pattern, repl, s)


On your example, I get:

>>> dict_replace('a b c', {'a': 'x', 'b': 'y'})

'x y c'
>>>




[1] http://python.org/doc/2.4.1/lib/node114.html
 
Reply With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      09-04-2005
Will McGugan wrote:
> Hi,
>
> Is there a simple way of replacing a large number of substrings in a
> string? I was hoping that str.replace could take a dictionary and use it
> to replace the occurrences of the keys with the dict values, but that
> doesnt seem to be the case.
>
> To clarify, something along these lines..
>
> >>> dict_replace( "a b c", dict(a="x", b="y") )

> "x y c"


(n.b. untested!)

def dict_replace(string, replacements):
for key, value in replacements.iteritems():
string = string.replace(key, value)
return string

How well this works depends on how large is "large." If "large" is
really very large, then you might want to build something using a more
suitable algorithm like the Aho-Corasick algorithm.

http://www.lehuen.com/nicolas/download/pytst/
http://hkn.eecs.berkeley.edu/~dyoo/python/ahocorasick/

--
Robert Kern
http://www.velocityreviews.com/forums/(E-Mail Removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

 
Reply With Quote
 
Michael J. Fromberger
Guest
Posts: n/a
 
      09-04-2005
In article <431af96c$0$29438$(E-Mail Removed)>,
Will McGugan <(E-Mail Removed)> wrote:

> Hi,
>
> Is there a simple way of replacing a large number of substrings in a
> string? I was hoping that str.replace could take a dictionary and use it
> to replace the occurrences of the keys with the dict values, but that
> doesnt seem to be the case.
>
> To clarify, something along these lines..
>
> >>> dict_replace( "a b c", dict(a="x", b="y") )

> "x y c"


Hi, Will,

Perhaps the following solution might appeal to you:

.. import re
..
.. def replace_many(s, r):
.. """Replace substrings of s. The parameter r is a dictionary in
.. which each key is a substring of s to be replaced and the
.. corresponding value is the string to replace it with.
.. """
.. exp = re.compile('|'.join(re.escape(x) for x in r.keys()))
.. return exp.sub(lambda m: r.get(m.group()), s)

Cheers,
-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing substrings with images in GUI (parser) Karsten Wutzke Java 3 03-20-2008 12:49 AM
Finding and Replacing Substrings In A String DarthBob88 C Programming 7 09-23-2007 03:14 PM
replacing substrings within strings amadain Python 11 02-14-2007 02:31 PM
Replacing palindrome substrings of an input string with a given string Tung Chau C Programming 1 08-06-2004 07:27 PM
Replacing palindrome substrings of an input string with a given string. Any effecient algorithm? Tung Chau C Programming 0 08-06-2004 10:18 AM



Advertisments