integer >= 1 == True and integer.0 == False is bad, bad, bad!!!

Paul Rubin no.email at nospam.invalid
Sun Jul 11 03:39:22 EDT 2010


rantingrick <rantingrick at gmail.com> writes:
> unspeakably ugly code.

I'd write the code differently to not do all those branches.
I like to use 1-elemnt lists as an option type, instead of using None,
so you can just concatenate them together to get the first non-empty
one.  Untested code:

    array = [c1,c2,c3,c4,c5,c6,...]

    # return first element of iterable that matches condition, wrapped
    # as a 1-element list.  If no match, return empty list.
    def xfind(condition, iterable):
      for x in iterable:
        if condition(x): return [x]
      return []

    while looping:
      cs = xfind(this_condition, array) + xfind(other_condition, array)
      # cs is now a list of either zero, one, or two matching elements
      if len(cs) == 1:
         r = cs[0]
      elif len(cs) = 2:
         r = <whichever is best>
      else:
         break
      best = array.pop(r)
      do_somthing_with(best)

Obviously you can golf the above in various ways.



More information about the Python-list mailing list