title: Finding a sequence of alternating numbers in a list
body: <p>I'm currently trying to implement the Fourth Nelson rule from:
  <a href="https://en.wikipedia.org/wiki/Nelson_rules" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Nelson_rules</a></p>
  
  <p>I.e. given a list of numbers of length N, I want to know if there exists a consecutive sequence of numbers that are alternating in direction of length n.  'Alternating' means consecutive numbers go up, then down, then up, etc.</p>
  
  <p>My data is in (t,x) tuples. 't' stands for the time axis, always increasing. 'x' is the value associated with the time and the series we are concerned with.  For example:</p>
  
  <pre><code>data = [(0, 2.5), (1, 2.1), (2, 1.7), (3, 2.0), (4, 0.3), (5, 0.8), (6, -1.2), (7, -0.5)]
  </code></pre>
  
  <p>Here, the alternating x value sequence is for everything but the first tuple.  See the below graph:
  <img src="https://i.stack.imgur.com/V2RbE.png" alt="Alternating Sequence in Red"></p>
  
  <p>The alternating sequence is highlighted in red.  The rule looks for 14 points in a row, but I want to generalize this to n-points in a row. (n &lt; N)  I can't just output True or False, I want to output the tuple of points that satisfy the condition.  In other words, the output would be:</p>
  
  <pre><code>outliers = [(1, 2.1), (2, 1.7), (3, 2.0), (4, 0.3), (5, 0.8), (6, -1.2), (7, -0.5)]
  </code></pre>
  
  <p>I've tried a few things, none of which resulted in the desired output.  These included things like np.diff() and np.sign().  I have a feeling itertools() can do this, but I can't quite get there.</p>
  
  <p>Any input is greatly appreciated.</p>