<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-15">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 16-Aug-2012 15:02, Peter Otten
wrote:<br>
</div>
<blockquote cite="mid:k0ir1b$eb3$1@ger.gmane.org" type="cite">
<pre wrap="">Virgil Stokes wrote:
</pre>
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">def testFunc(startingList):
xOnlyList = []; j = -1
for xl in startingList:
if (xl[0] == 'x'):
</pre>
</blockquote>
<pre wrap="">That's going to fail in the starting list contains an empty string. Use
xl.startswith('x') instead.
</pre>
</blockquote>
<pre wrap="">Yes, but this was by design (tacitly assumed that startingList was both a
list and non-empty).
</pre>
</blockquote>
<pre wrap="">You missunderstood it will fail if the list contains an empty string, not if
the list itself is empty:
</pre>
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">words = ["alpha", "", "xgamma"]
[word for word in words if word[0] == "x"]
</pre>
</blockquote>
</blockquote>
</blockquote>
<pre wrap="">Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
The startswith() version:
</pre>
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">[word for word in words if word.startswith("x")]
</pre>
</blockquote>
</blockquote>
</blockquote>
<pre wrap="">['xgamma']
Also possible:
</pre>
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">[word for word in words if word[:1] == "x"]
</pre>
</blockquote>
</blockquote>
</blockquote>
<pre wrap="">['xgamma']
</pre>
<blockquote type="cite">
<pre wrap="">def testFunc1(startingList):
'''
Algorithm-1
Note:
One should check for an empty startingList before
calling testFunc1 -- If this possibility exists!
'''
return([x for x in startingList if x[0] == 'x'],
[x for x in startingList if x[0] != 'x'])
I would be interested in seeing code that is faster than algorithm-1
</pre>
</blockquote>
<pre wrap="">In pure Python? Perhaps the messy variant:
def test_func(words):
nox = []
append = nox.append
withx = [x for x in words if x[0] == 'x' or append(x)]
return withx, nox
</pre>
</blockquote>
Very nice Peter,<br>
<br>
Here are the new results for timing with your method added
(algorithm-3).<br>
<br>
<table border="1" cellpadding="2" cellspacing="2" height="191"
width="542">
<tbody>
<tr>
<td valign="top">Method<br>
</td>
<td valign="top">average (sd) time in seconds<br>
</td>
</tr>
<tr>
<td valign="top">algorithm-1 (list comprehension)<br>
</td>
<td valign="top">0.11774 (0.002968)<br>
</td>
</tr>
<tr>
<td valign="top">algorithm-2 (S. D'Aprano)<br>
</td>
<td valign="top">0.17573 (0.003385)<br>
</td>
</tr>
<tr>
<td valign="top">algorithm-2A (modified S. D'Aprano)<br>
</td>
<td valign="top">0.18116 (0.003081)<br>
</td>
</tr>
<tr>
<td valign="top">algorithm-3 (improved list comprehension)<br>
</td>
<td valign="top">0.06639 (0.001728)<br>
</td>
</tr>
</tbody>
</table>
<br>
Algorithm-3 is 43% faster than algorithm-1. Again, the code used to
obtain these results is attached.<br>
<br>
Thanks Peter for your contribution <span class="moz-smiley-s1"></span>
</body>
</html>