Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We can do away with questions 2 and 3 by answering question 1, but understanding question 3 is important so I'll do that first and then show how you can avoid it entirely:</p> <h2>Question 3</h2> <p>As to question 3, it's because when you loop over a list in python and make changes to it <em>within the loop</em>, the list that you loop over changes.</p> <p>From the <a href="http://docs.python.org/tutorial/controlflow.html" rel="nofollow">python docs on control flow (for statement section)</a>:</p> <blockquote> <p>It is <strong>not safe</strong> to modify the sequence being iterated over in the loop (this can only happen for mutable sequence types, such as lists).</p> </blockquote> <p>Say your list is <code>[a,b,c,d]</code> and you loop through it with <code>for el in List</code>. Say <code>el</code> is currently <code>a</code> and you do <code>List.remove(el)</code>.</p> <p>Now, your list is <code>[b,c,d]</code>. However, the iterator points to the second element in the list (since it's done the first), which is now <code>c</code>. In essence, you've skipped <code>b</code>. So the problem is that you are modifying the list you are iterating over.</p> <p>There are a few ways to fix this: if your <code>List</code> is not expensive to duplicate, you could make a copy. So iterate over <code>List[:]</code> but remove from <code>List</code>.</p> <p>But suppose it's expensive to make copies of <code>List</code> all the time. Then what you do is iterate over it <strong>backwards</strong>. Note the <code>reversed</code> below:</p> <pre><code>for el in reversed(List): if el.count('$') &lt; n: counter+=1 List.remove(el) return(List) </code></pre> <p>In the example above, suppose we iterate backwards over <code>List</code>. The iterator starts at <code>d</code>, and then goes to <code>c</code>. Suppose we remove <code>c</code>, so that <code>List=[a,b,d]</code>. Since the iterator is going <em>backwards</em>, it now points to element <code>b</code>, so we haven't skipped anything.</p> <p>Basically, this avoids modifying bits of the list you have yet to iterate over.</p> <h2>Questions 1 &amp; 2</h2> <p>If I understand your question correctly, you basically want to choose <code>n</code> out of <code>m</code> positions, where <code>m</code> is the length of the string (<code>abcde</code>), and place a '$' in each of these <code>n</code> positions.</p> <p>In that case, you can use the <code>itertools</code> module to do that.</p> <pre><code>import itertools def Mismatch(Search,n): SearchL = list(Search) List = [] # hold output # print list of indices to replace with '$' idxs = itertools.combinations(range(len(SearchL)),n) # for each combination `idx` in idxs, replace str[idx] with '$': for idx in idxs: str = SearchL[:] # make a copy for i in idx: str[i]='$' List.append( ''.join(str) ) # convert back to string return List </code></pre> <p>Let's look at how this works:</p> <ol> <li>turn the <code>Search</code> string into a list so it can be iterated over, create empty <code>List</code> to hold results.</li> <li><p><code>idxs = itertools.combinations(range(len(SearchL)),n)</code> says "find all subsets of length n in the set <code>[0,1,2,3,...,length-of-search-string -1]</code>. Try </p> <pre><code>idxs = itertools.combinations(range(5),4) for idx in idxs: print idx </code></pre> <p>to see what I mean.</p></li> <li>Each element of <code>idxs</code> is a tuple of <code>n</code> indices from 0 to <code>len(SearchL)-1</code> (e.g. <code>(0,1,2,4)</code>. Replace the i'th character of <code>SearchL</code> with a '$' for each <code>i</code> in the tuple.</li> <li>Convert the result back into a string and add it to <code>List</code>.</li> </ol> <p>As an example:</p> <pre><code>Mismatch('abcde',3) ['$$$de', '$$c$e', '$$cd$', '$b$$e', '$b$d$', '$bc$$', 'a$$$e', 'a$$d$', 'a$c$$', 'ab$$$'] Mismatch('abcde',4) # note, the code you had made lots of duplicates. ['$$$$e', '$$$d$', '$$c$$', '$b$$$', 'a$$$$'] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload