A and B but not C in list

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jan 23 18:21:31 EST 2011


On 2011年1月23日 22:34:33 +0100, Christian Heimes wrote:
> It's easier and faster if you convert the lists to sets first:
>> your_set = set(your_list)
>> if your_set.issuperset(set([A, B])) and your_set.isdisjoint(set([C,
> D])):
> ...

"Easier" is a close thing. I find this easier to remember and write than 
set processing, even if it is a couple of characters longer:
if all(x in your_list for x in (A, B)) and not any(x in your_list for x 
in (C, D)):
 ...
And as for "faster", surely that will depend on the number of elements in 
your_list? The conversion from list to set doesn't happen for free, and 
it's likely that for small enough lists, that time may exceed any time 
savings you would otherwise gain.
-- 
Steven


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /