有没有办法检测 python 中的 True 到 False 转换?

Is there a way to detect True to False transitions in python?

我正在使用下面的代码来检测从 False 到 True 的转换。我需要单独的代码来检测从 True 到 False 的变化。有没有办法用下面的当前代码来做到这一点?

test = [True, False, False, True, True, False, True, True, False, False, False, True]

class EdgeDetector:
     """Detects False to True transitions on an external signal."""

     def __init__(self, reader, action):
         self.reader = reader
         self.action = action
         self.last_value = reader()    # initialise value

     def test(self):
         new_value = self.reader()
         if new_value and not self.last_value:
         self.action()
         self.last_value = new_value

 # simple self-test
 if __name__ == '__main__':
     vlist = test
     vgen = (v for v in vlist)              # generator for value sequence

     def test_reader():                     # generate test sequence
        value = next(vgen)
        print("Read:", value)
        return value

     def printer():
        print("Phase transition")

    test_subject = EdgeDetector(test_reader, printer)
    for i in range(len(vlist)-1):
         test_subject.test()

注意:这不是我的代码。我正在使用此线程提供的代码:

我不能 post 在该线程中提出后续问题。 感谢任何帮助。

问题就在这里if new_value and not self.last_value:

所以第一部分是寻找 new_value 是否有 Truthy 值。并且它正在寻找 self.last_value 是否具有 Falsey 值。当您开始比较两个不同的布尔值时,这会变得混乱。

第一部分实际上是在查看 new_value 中是否有值。所以我们可以将它与 none 进行比较。第二部分实际上是查看两个布尔值是否匹配,因此我们可以将它们相互比较。

将行更新为此,它应该适用于任一方向的转换。

if new_value is not None and new_value is not self.last_value:

编辑:

根据您的意见: 有几种方法我们可以跟踪相变是什么,而不必逐一查找。

选项一:

使用一个函数来跟踪从 true 到 false 和从 false 到 true 的转换。我们可以将值传回打印机函数。

def test(self):
    new_value = self.reader()
    if new_value is not None and new_value is not self.last_value:
        self.action(new_value) # Note here we are passing a value back

    self.last_value = new_value

现在我们只要添加打印机功能就可以看到变化了

def printer(transition):
   print(f"Phase transition to: {transition}")

最终代码选项 1:

test = [True, False, False, True, True, False, True, True, False, False, False, True]

class EdgeDetector:
     """Detects False to True transitions on an external signal."""

     def __init__(self, reader, action):
         self.reader = reader
         self.action = action
         self.last_value = reader()    # initialize value

     def test(self):
         new_value = self.reader()
         if new_value is not None and new_value is not self.last_value:
             self.action(new_value)

         self.last_value = new_value



# simple self-test
if __name__ == '__main__':
    vlist = test
    vgen = (v for v in vlist)              # generator for value sequence

    def test_reader():                     # generate test sequence
       value = next(vgen)
       print("Read:", value)
       return value

    def printer(transition):
       print(f"Phase transition to: {transition}")

    test_subject = EdgeDetector(test_reader, printer)
    for i in range(len(vlist)-1):
        test_subject.test()

选项二:

如果您想要为从 false 到 true 的转换和从 true 到 false 的转换分别编写代码,可以使用这种方法。

首先在 EdgeDetector class 我们需要修改 init 如下:

def __init__(self, reader, action_true, action_false): #Note the second action here
    self.reader = reader
    self.action_true = action_true
    self.action_false = action_false # We set it just like the previous one.
    self.last_value = reader()

那我们就可以更新测试代码了

def test(self):
    new_value = self.reader()
    if new_value is not None and new_value is not self.last_value:
        if new_value:
            self.action_true()
        else:
            self.action_false()

    self.last_value = new_value

然后我们修改打印机语句,添加第二个:

def printer_true():
    print("Phase transition to true")
    
def printer_false():
    print("Phase transition to false")

最后需要更新EdgeDetector的初始化。

test_subject = EdgeDetector(test_reader, printer_true, printer_false)

选项 2 的最终代码

test = [True, False, False, True, True, False, True, True, False, False, False, True]

class EdgeDetector:
     """Detects False to True transitions on an external signal."""

     def __init__(self, reader, action_true, action_false):
         self.reader = reader
         self.action_true = action_true
         self.action_false = action_false
         self.last_value = reader()    # initialize value

     def test(self):
         new_value = self.reader()
         if new_value is not None and new_value is not self.last_value:
             self.action(new_value)

         self.last_value = new_value



# simple self-test
if __name__ == '__main__':
    vlist = test
    vgen = (v for v in vlist)              # generator for value sequence

    def test_reader():                     # generate test sequence
       value = next(vgen)
       print("Read:", value)
       return value

    def printer_true():
       print(f"Phase transition to true")

    def printer_false():
       print(f"Phase transition to false")

    test_subject = EdgeDetector(test_reader, printer_true, printer_false)
    for i in range(len(vlist)-1):
        test_subject.test()

思路:需要检测False,True模式,表达式为~a & b。 要对齐输入,请使用 input[1:] 和 input[:-1],因此 ~input[:-1] & input[1:].

test = [True, False, False, True, True, False, True, True, False, False, False, True]

def edge_detector(inp):
    import numpy as np
    inp = np.array(inp)
    return np.arange(len(inp)-1)[~inp[:-1] & inp[1:]]

print(edge_detector(test))