Python:检查列表项是否有补码

Python:检查列表项是否有补码,python,python-2.7,Python,Python 2.7,我有一份清单: listA = ['P', 'Q', ['not', 'R'], ['not', 'S']] Input1 : ['not','P'] - Return True as complement exists Input2 : 'S' - Return True as complement exists 我想确定上面的列表(listA)中是否存在['not','p'](对p的赞美)。它在本例中存在,因此应该返回True 如何在python中实现这

我有一份清单:

 listA = ['P', 'Q', ['not', 'R'], ['not', 'S']]

 Input1 : ['not','P']    - Return True as complement exists
 Input2 : 'S'            - Return True as complement exists
我想确定上面的列表(listA)中是否存在['not','p'](对p的赞美)。它在本例中存在,因此应该返回True

如何在python中实现这一点?谢谢

def comp(el):
    if type(el) == str:
        return ['not', el]
    else:
        return el[1]


listA = ['P', 'Q', ['not', 'R'], ['not', 'S']]
comp(['not', 'P']) in listA  # True
comp('S') in listA  # True
不过,更好的方法可能是将逻辑值封装在类中:

class Logic_Value(object):
    def __init__(self, name, negation=False):
        self.name = name
        self.negation = negation

    def __neg__(self):
        return Logic_Value(self.name, not self.negation)

    def __str__(self):
        return '~' + self.name if self.negation else self.name
然后检查否定是否在列表中变为:

P = Logic_Value('P')
-P in listA  # True if not P is in listA