Re: substitution

Discussion in 'Python' started by Peter Otten, Jan 18, 2010.

  1. Peter Otten

    Peter Otten Guest

    superpollo wrote:

    > superpollo ha scritto:
    >> hi.
    >>
    >> what is the most pythonic way to substitute substrings?
    >>
    >> eg: i want to apply:
    >>
    >> foo --> bar
    >> baz --> quux
    >> quuux --> foo
    >>
    >> so that:
    >>
    >> fooxxxbazyyyquuux --> barxxxquuxyyyfoo
    >>
    >> bye

    >
    > i explain better:
    >
    > say the subs are:
    >
    > quuux --> foo
    > foo --> bar
    > baz --> quux
    >
    > then i cannot apply the subs in sequence (say, .replace() in a loop),
    > otherwise:
    >
    > fooxxxbazyyyquuux --> fooxxxbazyyyfoo --> barxxxbazyyybar -->
    > barxxxquuxyyybar
    >
    > not as intended...


    If you want to avoid regular expressions:

    def replace_many(s, pairs):
    if len(pairs):
    a, b = pairs[0]
    rest = pairs[1:]
    return b.join(replace_many(t, rest) for t in s.split(a))
    else:
    return s

    assert replace_many("abc", ["ab", "bc", "ca"]) == "bca"
    assert (replace_many("fooxxxbazyyyquuux",
    [("quuux", "foo"), ("foo", "bar"), ("baz", "quux")])
    == "barxxxquuxyyyfoo")

    Not tested.

    Peter
     
    Peter Otten, Jan 18, 2010
    #1
    1. Advertising

  2. Peter Otten

    Wyrmskull Guest

    Peter Otten wrote:

    def replace_many(s, pairs):
    if len(pairs):
    a, b = pairs[0]
    rest = pairs[1:]
    return b.join(replace_many(t, rest) for t in s.split(a))
    else:
    return s

    -------------

    Proves wrong, this way x -> y -> z.
    You call replace_many again on the central part of the split
    Specifics indicate that x -> y in the end.
    Your flowing pythonicity (if len(x):) gave me lots of inspiration.
    I bet this will win the prize ;)

    -------------

    def mySubst(reps,string):
    if not(len(reps)):
    return string
    a,b,c = string.partition(reps[0][0])
    if b:
    return mySubst(reps,a) + reps[0][1] + mySubst (reps,c)
    else:
    return mySubst(reps[1:],string)

    print mySubst( ( ('foo','bar'), ('bar','qux'), ('qux','foo') ), 'foobarquxfoo')

    -------
    Wyrmskull <>
     
    Wyrmskull, Jan 19, 2010
    #2
    1. Advertising

  3. Peter Otten

    Peter Otten Guest

    Wyrmskull wrote:

    > Peter Otten wrote:


    >> def replace_many(s, pairs):
    >> if len(pairs):
    >> a, b = pairs[0]
    >> rest = pairs[1:]
    >> return b.join(replace_many(t, rest) for t in s.split(a))
    >> else:
    >> return s


    > Proves wrong, this way x -> y -> z.
    > You call replace_many again on the central part of the split
    > Specifics indicate that x -> y in the end.


    Sorry, I don't understand what you want to say with the above.

    > Try with this:
    >
    > def mySubst(reps,string):
    > if not(len(reps)):
    > return string
    > current = reps[0][0]
    > a,b,c = string.partition(current)
    > if b:
    > return mySubst(reps,a) + reps[0][1] + mySubst (reps,c)
    > else:
    > return mySubst(reps[1:],string)
    >
    > print mySubst( ( ('foo','bar'), ('bar','qux'), ('qux','foo') ),
    > 'foobarquxfoo')
    >
    > -------
    > Wyrmskull <>


    I don't see at first glance where the results of replace_many() and
    mySubst() differ. Perhaps you could give an example?

    Peter

    PS: Please keep the conversation on-list
     
    Peter Otten, Jan 19, 2010
    #3
  4. Peter Otten

    Peter Otten Guest

    Wyrmskull wrote:

    > Your flowing pythonicity (if len(x):) gave me lots of inspiration.


    Actually, instead of

    if len(pairs): ...

    that should have been just

    if pairs: ...

    When I started writing the function I initially wanted to special-case
    len(pairs) == 1. The len() call is a superfluous leftover.

    Peter
     
    Peter Otten, Jan 19, 2010
    #4
  5. Peter Otten

    Wyrmskull Guest

    Nvm, my bad, I misunderstood the split instruction.
    No difference :)

    -------
    Wyrmskull

    P.S Sorry about the P.M., I misclicked on a GUI
     
    Wyrmskull, Jan 19, 2010
    #5
    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. valentin tihomirov

    Should this substitution be compilable?

    valentin tihomirov, Nov 28, 2004, in forum: VHDL
    Replies:
    12
    Views:
    793
    valentin tihomirov
    Nov 30, 2004
  2. Troll
    Replies:
    6
    Views:
    2,437
    Kris Wempa
    Sep 26, 2003
  3. Justin

    adobe multiline substitution

    Justin, Dec 8, 2003, in forum: Perl
    Replies:
    0
    Views:
    501
    Justin
    Dec 8, 2003
  4. Ashok

    Substitution Problem

    Ashok, Jul 18, 2004, in forum: Perl
    Replies:
    1
    Views:
    654
    Gunnar Hjalmarsson
    Jul 18, 2004
  5. Ian
    Replies:
    4
    Views:
    2,308
    Ben Bacarisse
    Feb 2, 2006
Loading...

Share This Page