text: user3515: I have a string, a list of substrings, and a list of positions where substrings are found in string.
  string = 'ATGGTACACGCTACGAGCTAG'
  substrings = ['TACA', 'CTAGAAAAG']
  positions = ['11, 4', '']
  
  Substring 'TACA' appears at positions 11 and 4 in string (i.e. '11, 4').
  Substring 'CTAGAAAAG' does not appear in string (i.e. '').
  I need to remove 'CTAGAAAAG' from substrings and I need to remove '' from positions, while creating a new list with 'CTAGAAAAG'. I need your help here to create code which will give the following results:
  new_substrings = ['TACA']
  new_positions = ['11, 4']
  new_list = ['CTAGAAAAG']
  string = 'ATGGTACACGCTACGAGCTAG' (obtained from above)
  
  Once this is complete, the given code:
  new_positions = [map(int, x.split(',')) for x in new_positions]
  seq_pos = dict(zip(new_substrings,new_positions))
  seq_ref = {k: [string[start:start+length] for start in starts] for k, starts, length in zip(new_substrings,new_positions,[len(x) for x in new_substrings])}
  
  Gives:
  seq_pos = {'TACA': [11, 4]}
  seq_ref = {'TACA': ['TACG', 'TACA']}
  
  Then, from seq_ref, I need to create a list of integers which represents the number of mismatches between the key: 'TACA' and both of its values: 'TACG' and 'TACA'.
  mismatches = [[1, 0]]
  
  The code to find the # of mismatches between two strings is provided:
  def HammingDistance(p, q):
      dist = 0
      for i in range(len(p)):
          if p[i] != q[i]:
              dist += 1
      return dist
  
  Finally, I need to save my data in a CSV file (i.e. Excel or Numbers).
  new_positions = ['11, 4'] (obtained above)
  new_substrings = ['TACA'] (obtained above)
  actual_substrings = [['TACG', 'TACA']] (values of seq_ref)
  mismatches = [[1, 0]] (obtained above)
  
  Info needs to be sorted based on # of mismatches.
  
  I understand this is a multi-step problem but any help would be appreciated.
  
  user964: For the first question try something like:
  new_substrings = []
  new_positions = []
  new_list = []
  for pos, sub in zip(positions, substrings): 
     if not pos:
         new_list.append(sub)
     else:
         new_substrings.append(sub)
         new_positions.append(pos)
  
  this gives:
  >>> new_substrings
  ['TACA']
  >>> new_positions
  ['11, 4']
  >>> new_list
  ['CTAGAAAAG']
  
  For the hamming distance you just need to iterate over the values in seq_ref and append the results of the hamming distance function in mismatches.  Based on the given code and the example above you should be able to create that block.  Below is an untested quick example to get you on track.  
  mismatches = []
  for seq in seq_ref.keys():
     for value in seq_ref[seq]:
         mismatches.append(HammingDistance(seq, value))
  
  As far as the CSV and sorting is concerned I suggest that you look into pandas or numpy.  Both of those packages provided functions to help with this.
  
original_text: 3515: <p>I have a string, a list of substrings, and a list of positions where substrings are found in string.</p>
  
  <pre><code>string = 'ATGGTACACGCTACGAGCTAG'
  substrings = ['TACA', 'CTAGAAAAG']
  positions = ['11, 4', '']
  </code></pre>
  
  <p>Substring <code>'TACA'</code> appears at positions 11 and 4 in string (i.e. <code>'11, 4'</code>).</p>
  
  <p>Substring <code>'CTAGAAAAG'</code> does not appear in string (i.e. <code>''</code>).</p>
  
  <p>I need to remove <code>'CTAGAAAAG'</code> from <code>substrings</code> and I need to remove <code>''</code> from <code>positions</code>, while creating a new list with <code>'CTAGAAAAG'</code>. <strong>I need your help here to create code which will give the following results:</strong></p>
  
  <pre><code>new_substrings = ['TACA']
  new_positions = ['11, 4']
  new_list = ['CTAGAAAAG']
  string = 'ATGGTACACGCTACGAGCTAG' (obtained from above)
  </code></pre>
  
  <p>Once this is complete, the <strong>given</strong> code:</p>
  
  <pre><code>new_positions = [map(int, x.split(',')) for x in new_positions]
  seq_pos = dict(zip(new_substrings,new_positions))
  seq_ref = {k: [string[start:start+length] for start in starts] for k, starts, length in zip(new_substrings,new_positions,[len(x) for x in new_substrings])}
  </code></pre>
  
  <p>Gives:</p>
  
  <pre><code>seq_pos = {'TACA': [11, 4]}
  seq_ref = {'TACA': ['TACG', 'TACA']}
  </code></pre>
  
  <p>Then, from seq_ref, I need to create a list of integers which represents the number of mismatches between the key: <code>'TACA'</code> and both of its values: <code>'TACG'</code> and <code>'TACA'</code>.</p>
  
  <pre><code>mismatches = [[1, 0]]
  </code></pre>
  
  <p>The code to find the # of mismatches between two strings is provided:</p>
  
  <pre><code>def HammingDistance(p, q):
      dist = 0
      for i in range(len(p)):
          if p[i] != q[i]:
              dist += 1
      return dist
  </code></pre>
  
  <p>Finally, I need to save my data in a CSV file (i.e. Excel or Numbers).</p>
  
  <pre><code>new_positions = ['11, 4'] (obtained above)
  new_substrings = ['TACA'] (obtained above)
  actual_substrings = [['TACG', 'TACA']] (values of seq_ref)
  mismatches = [[1, 0]] (obtained above)
  </code></pre>
  
  <p>Info needs to be sorted based on # of mismatches.</p>
  
  <p><a href="https://i.stack.imgur.com/LdAa1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LdAa1.png" alt="enter image description here"></a></p>
  
  <p>I understand this is a multi-step problem but any help would be appreciated.</p>
   964: <p>For the first question try something like:</p>
  
  <pre><code>new_substrings = []
  new_positions = []
  new_list = []
  for pos, sub in zip(positions, substrings): 
     if not pos:
         new_list.append(sub)
     else:
         new_substrings.append(sub)
         new_positions.append(pos)
  </code></pre>
  
  <p>this gives:</p>
  
  <pre><code>&gt;&gt;&gt; new_substrings
  ['TACA']
  &gt;&gt;&gt; new_positions
  ['11, 4']
  &gt;&gt;&gt; new_list
  ['CTAGAAAAG']
  </code></pre>
  
  <p>For the hamming distance you just need to iterate over the values in <code>seq_ref</code> and append the results of the hamming distance function in <code>mismatches</code>.  Based on the given code and the example above you should be able to create that block.  Below is an untested quick example to get you on track.  </p>
  
  <pre><code>mismatches = []
  for seq in seq_ref.keys():
     for value in seq_ref[seq]:
         mismatches.append(HammingDistance(seq, value))
  </code></pre>
  
  <p>As far as the CSV and sorting is concerned I suggest that you look into pandas or numpy.  Both of those packages provided functions to help with this.</p>
  