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 10:31:08 CEST 2010


Not sure why you use the for-else syntax without a break or continue. And
I'm also not sure on the readability.

-Xav on his Froyo
On 29/10/2010 6:21 PM, "HEK" <elkarouh at gmail.com> wrote:
> On Oct 28, 6:16 pm, "cbr... at cbrownsystems.com"
> <cbr... at cbrownsystems.com> wrote:
>> It's clear but tedious to write:
>>
>> if 'monday" in days_off or "tuesday" in days_off:
>>     doSomething
>>
>> I currently am tending to write:
>>
>> if any([d for d in ['monday', 'tuesday'] if d in days_off]):
>>     doSomething
>>
>> Is there a better pythonic idiom for this situation?
>>
>> Cheers - Chas
>
> The most pythonic way is the following:
>
> class anyof(set):
> def __contains__(self,item):
> if isinstance(item,anyof):
> for it in item:
> if self.__contains__(it):
> return True
> else:
> return False
> return super(anyof,self).__contains__(item)
>
> daysoff=anyof(['Saterday','Sunday'])
> assert anyof(['monday','tuesday']) in daysoff
>
> best regards
> Hassan
> --
> http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101029/907a20d7/attachment.html>


More information about the Python-list mailing list