Python: if more than one of three things is true, return false

前端 未结 11 2520
温柔的废话
温柔的废话 2021-02-14 01:33

I\'m writing a django model that allows my site to have coupons.

Coupons can have three types: lifetime account voucher, certain period of months voucher, certain numb

11条回答
  •  孤独总比滥情好
    2021-02-14 01:54

    Even better solution than before, with combinations, any, and all. Assuming you have all the attributes you want to test in a sequence called attributes:

    from itertools import combinations
    any(map(all, combinations(attributes, 2)))
    

    In english, it reads

    Are any length-2 combinations of the attributes all true?

    This solution works for an arbitrary number of attributes, and can be modified to test for an arbitrary number of them being true.

    Although admittedly it's very inefficient, I'd say it's pretty cute and readable.

提交回复
热议问题