title: Finding patterns in list
body: <p>I am currently searching for a way to find patterns in a list of integers, but the method I am going to use would be applicable to strings and other lists with different elements of course. Now let me explain what I am looking for.</p>
  
  <p>I want to find the longest repeating pattern in a list of integers. For example,</p>
  
  <pre><code>[1, 2, 3, 4, 1, 2, 3]
  # This list would give 1, 2, 3
  </code></pre>
  
  <p>Overlapping patterns should be discarded. ( Not certain )</p>
  
  <pre><code>[1, 1, 1, 1, 1]
  # Should give 1, 1  Not 1, 1, 1, 1
  </code></pre>
  
  <p><strong>Here is what did not help me.</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/6656310/finding-patterns-in-a-list">Finding patterns in a list</a> (Did not understand the logic behind the first answer, very little explanation. And second answer solves the problem only if the pattern is known before solving.)</p>
  
  <p><a href="https://stackoverflow.com/questions/33928775/finding-integer-pattern-from-a-list">Finding integer pattern from a list</a> (Pattern is given and number of occurrence is wanted. Different than my question.)</p>
  
  <p><a href="https://en.wikipedia.org/wiki/Longest_common_subsequence_problem" rel="noreferrer">Longest common subsequence problem</a> (Most people dealed with this problem however it is not close to mine. I need consecutive elements while searching for a pattern. However in this, seperate elements also counted as subsequences.)</p>
  
  <p><strong>Here what I tried.</strong></p>
  
  <pre><code>def pattern(seq):
      n = len(seq)
      c = defaultdict(int) # Counts of each subsequence
      for i in xrange(n):
          for j in xrange(i + 1, min(n, n / 2 + i)): 
              # Used n / 2 because I figured if a pattern is being searched
              # It cant be longer that the half of the list.
              c[tuple(seq[i:j])] += 1      
      return c
  </code></pre>
  
  <p>As you see, It finds all the sublists and check for repeats. I found this approach a bit naive(and inefficient) and I am in need of a better way. Please help me. Thanks in advance.</p>
  
  <p><strong>Note1:</strong> The list is predetermined but because of my algorithms failure, I can only check some parts of the list before freezing the computer. So the pattern I am trying to find can very well be longer than the half of the search list, It can even be longer than the search list itself because I am searching only a part of the original list.If you present a better method than I am using, I can search a larger part of the original list so I will have a better chance at finding the pattern. (If there is one.)</p>
  
  <p><strong>Note2:</strong> Here is a part of the list if you want to test it yourself. It really seems like that there is a pattern, but I cannot be sure before I test it with a reliable code.
  <a href="http://collabedit.com/f5j3v" rel="noreferrer">Sample List</a></p>
  
  <p><strong>Note3:</strong> I approach this as a serious problem of data mining. And will try to learn if you make a long explanation. This feels like a much more important problem than LCS, however LCS is much more popular :D This method I am trying to find feels like the methods scientists use to find DNA patterns.</p>