RE: Rita Sue and Bob too

Discussion in 'Python' started by Ben Last, Aug 20, 2004.

  1. Ben Last

    Ben Last Guest

    > From Peter Hansen
    > > But, how to I find a sequence in a list of unknown size? i.e.

    > this sequence
    > > in list of other names and replace it with three others?
    > >
    > > 'Rita','Sue','Bob'
    > >
    > > This is almost a nightly occurrence (my posting questions), but I am
    > > learning : )


    This isn't necessarily the best way to do it generally, but it's an
    Interesting problem to play with, so here's an Interesting solution. Use
    the string facilities that already exist to search sequences (a string is a
    sequence too). All you need to is convert the lists to strings and back:

    seqToFind = ['Rita','Sue','Bob']
    seqToReplace = ['A','B','C']
    seqToSearch =
    ['Ben','Peter','Guido','Mark','David','Rita','Sue','Bob','Junk','Extra']

    #joiner should of course be a substring that doesn't occur in the list
    elements.
    joiner = "~~"

    stringToFind = string.join(seqToFind,joiner)
    stringToSearch = string.join(seqToSearch,joiner)
    stringToReplaceWith = string.join(seqToReplace,joiner)
    resultString = stringToSearch.replace(stringToFind,stringToReplaceWith)
    resultSeq = resultString.split(joiner)

    That last block could also be written as:

    #s ends up with the resulting list
    s = seqToSearch
    s =
    string.join(seqToSearch,joiner).replace(string.join(seqToFind,joiner),string
    ..join(seqToReplace,joiner)).split(joiner)

    But that's verging on the obfuscated :)

    regards,
    b
     
    Ben Last, Aug 20, 2004
    #1
    1. Advertising

  2. Ben Last

    Peter Hansen Guest

    Ben Last wrote:

    >>From Peter Hansen
    >>
    >>>But, how to I find a sequence in a list of unknown size? i.e.
    >>>this sequence in list of other names and replace it with three others?
    >>>
    >>>'Rita','Sue','Bob'
    >>>
    >>>This is almost a nightly occurrence (my posting questions), but I am
    >>>learning : )


    Actually, that wasn't quite me who wrote those words... I merely
    quoted them from the OP.

    (Ben, I don't think you need to post to both the python-list _and_
    the comp.lang.python newsgroup: they mirror each other already!)

    -Peter
     
    Peter Hansen, Aug 20, 2004
    #2
    1. Advertising

  3. Ben Last

    Peter Hansen Guest

    Ben Last wrote:

    >>From Peter Hansen
    >>
    >>>But, how to I find a sequence in a list of unknown size? i.e.
    >>>this sequence in list of other names and replace it with three others?
    >>>
    >>>'Rita','Sue','Bob'
    >>>
    >>>This is almost a nightly occurrence (my posting questions), but I am
    >>>learning : )


    Actually, that wasn't quite me who wrote those words... I merely
    quoted them from the OP.

    (Ben, I don't think you need to post to both the python-list _and_
    the comp.lang.python newsgroup: they mirror each other already!)

    -Peter
     
    Peter Hansen, Aug 20, 2004
    #3
  4. Ben Last

    Neal Holtz Guest

    "Ben Last" <> wrote in message news:<>>
    > seqToFind = ['Rita','Sue','Bob']
    > seqToReplace = ['A','B','C']
    > seqToSearch =
    > ['Ben','Peter','Guido','Mark','David','Rita','Sue','Bob','Junk','Extra']
    > ...
    >
    > That last block could also be written as:
    >
    > #s ends up with the resulting list
    > s = seqToSearch
    > s =
    > string.join(seqToSearch,joiner).replace(string.join(seqToFind,joiner),string
    > .join(seqToReplace,joiner)).split(joiner)
    >
    > But that's verging on the obfuscated :)


    Very perlish indeed.

    But the above fails if the first or last words in seqToFind are
    trailing
    or leading substrings of other words in seqToSearch. IE, if>
    seqToSearch =
    ['Ben','Peter','Guido','Mark','David','Rita','Sue','BobbyJoe','Junk','Extra']

    So something that fails slightly less often:

    def repseq4( names, old, new ):
    s = '\003'; return
    ((s+s.join(names)+s).replace(s+s.join(old)+s,s+s.join(new)+s,1)).split(s)[1:-1]

    It turns out that this is actually faster than the more obvious
    methods
    (by about 25% (over Jeremy Jones' method) for a names list of 1000
    names, replacing the middle 3).
    Sure is ugly, though.
     
    Neal Holtz, Aug 20, 2004
    #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. M. Clift

    Rita Sue and Bob too

    M. Clift, Aug 20, 2004, in forum: Python
    Replies:
    13
    Views:
    621
    Shane Holloway (IEEE)
    Aug 23, 2004
  2. Cyrille Lavigne

    Re: Rita Sue and Bob too

    Cyrille Lavigne, Aug 20, 2004, in forum: Python
    Replies:
    3
    Views:
    320
    Andrew Durdin
    Aug 21, 2004
  3. Ben Last

    RE: Rita Sue and Bob too

    Ben Last, Aug 20, 2004, in forum: Python
    Replies:
    2
    Views:
    385
    M. Clift
    Aug 20, 2004
  4. http://clearblogs.com/rita/24759/

    http://clearblogs.com/rita/24759/

    http://clearblogs.com/rita/24759/, Jan 5, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    336
    http://clearblogs.com/rita/24759/
    Jan 5, 2007
  5. Tim Streater

    @RH - Messrs Sue, Grabbit, and Runne

    Tim Streater, Jun 21, 2010, in forum: C Programming
    Replies:
    0
    Views:
    284
    Tim Streater
    Jun 21, 2010
Loading...

Share This Page