如何使用not和or简化布尔表达式?

时间:2017-10-25 08:42:34

标签: python algorithm boolean-logic

我有以下布尔表达式:

not (start_date > b or s > end_date)

如何简化它?

def is_date_in_items(end_date, start_date, items):
    b, s = _get_biggest_and_smallest_date(items)
    return not (start_date > b or s > end_date)

2 个答案:

答案 0 :(得分:6)

not (start_date > b or s > end_date)

// is equivalent to 
not(start_date > b) and not(s > end_date)

// which is equivalent to 
start_data <= b and s <= end_date

这来自De Morgan's Laws,其中指出:

¬(P OR Q) <=> (¬P) AND (¬Q) 

答案 1 :(得分:1)

你可以缩短一点:

start_date <= b and s <=end_date