A and B but not C in list
Peter Otten
__peter__ at web.de
Mon Jan 24 03:47:01 EST 2011
Ian Kelly wrote:
> On Sun, Jan 23, 2011 at 2:34 PM, Christian Heimes <lists at cheimes.de>
> wrote:
>> your_set = set(your_list)
>>
>> if your_set.issuperset(set([A, B])) and your_set.isdisjoint(set([C, D])):
>
> if your_set.intersection([A, B, C, D]) == set([A, B]):
> ...
You can avoid converting your_list to a set with (using 2.7/3.x notation)
if {A, B, C, D}.intersection(your_list) == {A, B}:
...
The items in your_list still have to be hashable, so the approach is not as
general as
if (all(required in your_list for required in (A, B)) and
not any(forbidden in your_list for forbidden in (C, D))):
...
or similar.
Also, it's not as easy to understand, so don't forget the explaining comment
if you use the set-based approach.
Peter
More information about the Python-list
mailing list