如何在Python列表中查找特定模式?

I have a list containing only True and False values. I am looking for a pattern when the elements of the list changes from True to False or vise versa more than 3 times.

Example (T is used for True and F for False for abbreviation):

List = [T, T, T, F, F, F, T, F, T, F, T, T, T, T] 

What I want to detect is : [F, T, F, T, F, T] and its starting index in the original list.

如果您有任何想法可以有效完成此任务,请告诉我。

If fact, I need this detection to be done in real-time. I mean, the List is being made by getting data from another source (timestamp is 0.5 second). And I need to detect the above mentioned pattern in the List. In you are aware how to solve this problem (either real time or not), please let me know.

评论
  • et_ea
    et_ea 回复

    好吧,您可以将每个列表变成一个字符串:

    newlist=['T' if x is True else 'F' for x in mylist]
    newstring=''.join(newlist)
    

    Then find the position of the match to 'FTFTFT' as described in the accepted answer to this question.