On 07/16/2010 02:20 PM, Chad Kellerman wrote:
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

While I like MRAB's solution even better than mine[1], you can also use:

  liveHostList = ["app11", "app12", "web11", "web12", "host11"]
  stageHostList = ["web21", "web22", "host21", "app21", "app22"]

  def bits(s):
    return (s[:-2],s[-1])

  for live, stage in zip(
      sorted(liveHostList, key=bits),
      sorted(stageHostList, key=bits),
      ):
    print "Match: ", live, stage

-tkc


[1] His solution is O(N), making one pass through each list, with O(1) lookups into the created dict during the 2nd loop, while mine is likely overwhelmed by the cost of the sorts...usually O(N log N) for most reasonable sorts. However, this doesn't likely matter much until your list-sizes are fairly large.




--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to