Pythonic way of saying 'at least one of a, b, or c is in some_list'

Xavier Ho contact at xavierho.com
Fri Oct 29 02:05:11 EDT 2010


On 29 October 2010 15:50, Paul Rubin <no.email at nospam.invalid> wrote:

> John Nagle <nagle at animats.com> writes:
> >    d1 = set('monday','tuesday')
> >    days_off = set('saturday','sunday')
> >    if not d1.isdisjoint(days_off) :...
> >    This is cheaper than intersection, since it doesn't have to
> > allocate and construct a set. It just tests whether any element in the
> > smaller of the two sets is in the larger one.
>
> I wonder what the complexity is, since the simplest implementation using
> the dict-like features of sets is quadratic.  There is of course an
> obvious n log n implementation involving sorting.
>

I like John Nagle's idea. Presuming isdisjoint() will return upon early
termination criteria, the average case is probably much better than O(n^2).
Best case is near linear, and worse case will probably never happen, because
if you have to cover every day, chances are they overlap. Sorting in this
case is probably not needed. Of course, we're only looking at 7 days a week,
so performance is not an issue.

If I really cared about performance, I would probably use bits and then do a
logical AND operation.

+1 to Nagle from me.

Cheers,
Xav
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101029/d3a706b0/attachment-0001.html>


More information about the Python-list mailing list