title: Finding a word and confirming the surrounding letters of a word in a continuous clump of letters efficiently
body: <p>I'm cutting up <code>letterlist</code> at the letters in <code>cutters</code>.</p>
  
  <p>The number in <code>cutspots</code> means that the letter in <code>cutters</code> of the same <code>position</code> cuts to the right of that letter, ie 0 would cut to the left.</p>
  
  <p>Following these rules I get results relevant to the words in <code>wordlist</code>, saying this generates 1 <code>'WORD'</code> and 0 <code>'ANOTHERK'</code>s. Although both are in there, they aren't generated by the cutting.</p>
  
  <p>This was originally decently fast, but I found myself, instead of just searching for the words in <code>wordlist</code> themselves, needing to confirm that a letter I was cutting up the list by was actually before and after my word in <code>letterlist</code>.</p>
  
  <p>This slows down quite a bit by needing to iterate over <code>cutters</code> to generate <code>cutters[position]</code>, is there any more clever way to figure out if my words in <code>wordlist</code> are flanked by the letters in <code>cutterlist</code> at the appropriate positions?</p>
  
  <pre><code>import re
  import itertools
  
  letterlist = ['GIURELABGIRUGHAWUWORDHGUOIHRUANOTHERKHVUHREIAVHRELGBEIUVIREUVBKWORDGOREGK']
  
  cutters = ['K', 'D']
  cutspots = [1, 1]
  
  wordlist = [['WORD'], ['ANOTHERK']]
  
  wordfinder = [[sum(x) for x in zip(*[[len(list(set(itertools.chain(*[[m.start() for m in re.finditer(wordlist[i][j], letterlist[h]) if cutspots[position] == 0 and letterlist[h].endswith(wordlist[i][j]) or letterlist[h][m.start()+len(wordlist[i][j])-abs(0-cutspots[position])] is cutters[position]] for position in range(len(cutters))])) &amp; set(itertools.chain(*[[m.start() for m in re.finditer(wordlist[i][j], letterlist[h]) if cutspots[position] == 1 and letterlist[h].startswith(wordlist[i][j]) or letterlist[h][m.start()-cutspots[position]] is cutters[position]] for position in range(len(cutters))])))) for j in range(len(wordlist[i]))] for h in range(len(letterlist))])] for i in range(len(wordlist))]
  </code></pre>