Replacing large number of substrings

Discussion in 'Python' started by Will McGugan, Sep 4, 2005.

  1. Will McGugan

    Will McGugan Guest

    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
     
    Will McGugan, Sep 4, 2005
    #1
    1. Advertising

  2. Will McGugan

    tiissa Guest

    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
     
    tiissa, Sep 4, 2005
    #2
    1. Advertising

  3. Will McGugan

    Robert Kern Guest

    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


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Sep 4, 2005
    #3
  4. In article <431af96c$0$29438$>,
    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"


    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
     
    Michael J. Fromberger, Sep 4, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. amadain
    Replies:
    11
    Views:
    438
    Paul McGuire
    Feb 14, 2007
  2. Tung Chau
    Replies:
    1
    Views:
    470
    SM Ryan
    Aug 6, 2004
  3. Tung Chau
    Replies:
    0
    Views:
    376
    Tung Chau
    Aug 6, 2004
  4. DarthBob88

    Finding and Replacing Substrings In A String

    DarthBob88, Sep 23, 2007, in forum: C Programming
    Replies:
    7
    Views:
    566
    Keith Thompson
    Sep 23, 2007
  5. Karsten Wutzke
    Replies:
    3
    Views:
    394
    Jeff Higgins
    Mar 20, 2008
Loading...

Share This Page