pattern matching with multiple lists

Chad Kellerman sunckell at gmail.com
Fri Jul 16 15:20:16 EDT 2010


Greetings,
     I have some code that I wrote and know there is a better way to
write it.  I  wonder if anyone could point me in the right direction
on making this 'cleaner'.

     I have two lists:   liveHostList = [ app11, app12, web11, web12, host11 ]
                                    stageHostList      = [  web21,
web22, host21, app21, app22 ]

     I need to pair the elements in the list such that:
    app11  pairs with app21
    app12 pairs with app22
    web11 pairs with web21
    web12 pairs with web22
    host11pairs with host21

    each time I get the list I don't know the order, and the lists
will grow over time.  (hosts will be added in pairs.  app13 to
liveHostList and app23 to stageHostList, etc)


Anyways this is what I have.  I think it can be written better with
map, but not sure.  Any help would be appreciated.

import re
for liveHost in liveHostlist:

    nameList = list(liveHost)
    clone    = nameList[-1]
    di       = nameList[-2]
    generic  = liveHost[:-2]

    for stageHost in stageHostList:
        if re.match( generic + '.' + clone, stageHost ):
            print "Got a pair: " + stageHost + liveHost

Thanks again for any suggestions,
Chad


--
A grasshopper walks into a bar and the bartender says "Hey, we have a
drink named after you." And the grasshopper says "Really, You have a
drink named Murray?"



More information about the Python-list mailing list