Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Regarding <strong>efficiency</strong>: The method you show has an <a href="http://en.wikipedia.org/wiki/Big_O_notation" rel="nofollow">asymptotic runtime complexity</a> of <code>O(m*n)</code> where <code>m</code> and <code>n</code> are the number of elements in <code>reference</code> and <code>done</code>, i.e. if you double the size of both lists, the algorithm will run 4 times longer (times a fixed constant that is uniteresting to theoretical computer scientists). If <code>m</code> and <code>n</code> are very large, you will probably want to choose a faster algorithm, e.g sort the two lists first using the <code>.sort()</code> (runtime complexity: <code>O(n * log(n))</code>) and then go through the lists just once (runtime complexity: <code>O(n)</code>). That algorithm has a worst-case runtime complexity of <code>O(n * log(n))</code>, which is already a big improvement. However, you trade readability and simplicity of the code for efficiency, so I would only advise you to do this if absolutely necessary.</p> <p>Regarding <strong>coding style</strong>: You do not <code>.close()</code> the file handles which you should. Instead of opening and closing the file handle, you could use the <a href="http://docs.python.org/reference/compound_stmts.html#the-with-statement" rel="nofollow"><code>with</code> language construct</a> of python. Also, if you like the functional style, you could replace the for loop by a list expression:</p> <pre><code>for i in reference: if i not in done: print i, </code></pre> <p>then becomes:</p> <pre><code>items = [i.strip() for i in reference if i not in done] print ' '.join(items) </code></pre> <p>However, this way you will not see any progress while the list is being composed.</p> <p>As joaquin already mentions, you can loop over <code>f</code> directly instead of <code>f.readlines()</code> as file handles support the <a href="http://www.python.org/dev/peps/pep-0234/" rel="nofollow">iterator protocol</a>.</p>
 

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