Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27166
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!border2.nntp.ams2.giganews.com!border1.nntp.ams2.giganews.com!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <vs@it.uu.se> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'author:': 0.03; 'else:': 0.03; 'string.': 0.04; 'append': 0.07; 'executed': 0.07; 'filename:fname piece:py': 0.07; 'sequences.': 0.07; 'skip:% 20': 0.07; 'python': 0.09; '#print': 0.09; "'''": 0.09; '32-bit': 0.09; '[];': 0.09; 'assumed': 0.09; 'behavior,': 0.09; 'notes:': 0.09; 'skip:# 60': 0.09; 'toc': 0.09; 'def': 0.10; 'index': 0.13; 'vista': 0.13; '(must': 0.16; 'indexerror:': 0.16; 'messy': 0.16; 'mug': 0.16; 'run.': 0.16; 'skip:[ 50': 0.16; 'true:': 0.16; 'string': 0.17; 'wrote:': 0.17; 'char': 0.17; 'element': 0.17; 'string,': 0.17; '>>>': 0.18; 'windows': 0.19; 'math': 0.20; 'parameters': 0.20; 'python?': 0.20; 'import': 0.21; '"",': 0.22; 'assignment': 0.22; 'clock': 0.22; 'delta': 0.22; 'displayed': 0.22; 'skip:% 10': 0.22; 'runs': 0.22; 'attached.': 0.23; 'elements': 0.23; 'task': 0.23; '(this': 0.24; 'random': 0.24; 'testing': 0.24; 'header:User-Agent:1': 0.26; 'charset:iso-8859-15': 0.26; '(most': 0.27; 'possibility': 0.27; 'instead.': 0.27; 'initial': 0.28; 'run': 0.28; '100000': 0.29; '>>>>': 0.29; 'cpu': 0.29; "d'aprano": 0.29; 'seed': 0.29; "skip:' 50": 0.29; 'steven': 0.29; 'character': 0.29; 'words': 0.29; 'skip:& 10': 0.29; '(from': 0.30; 'checks': 0.30; 'resolution': 0.30; 'seconds': 0.30; 'lists': 0.31; 'code': 0.31; 'skip:- 30': 0.31; 'file': 0.32; 'print': 0.32; 'traceback': 0.33; 'version:': 0.33; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'list': 0.35; 'clear': 0.35; 'fail': 0.35; 'false': 0.35; 'faster': 0.35; 'but': 0.36; 'characters': 0.36; 'generation': 0.36; 'skip:g 30': 0.36; 'email addr:python.org': 0.36; 'method': 0.36; 'should': 0.36; 'itself': 0.37; '(for': 0.37; 'passed': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'your': 0.60; 'easy': 0.60; 'range': 0.60; 'leading': 0.61; 'containing': 0.61; 'received:62': 0.62; 'email name:python-list': 0.62; 'strange': 0.62; 'different': 0.63; 'email addr:gmail.com': 0.63; 'note:': 0.64; 'here': 0.65; 'charset:windows-1252': 0.65; 'results': 0.65; '10,000': 0.65; '10000': 0.65; '100': 0.78; 'completion': 0.78; '50%': 0.81; 'avg': 0.84; "d'aprano)": 0.84; 'otten': 0.84; 'otten)': 0.84; 'peter,': 0.84; '100,000': 0.91; 'average': 0.93 |
| X-SENDER-IP | [213.112.50.224] |
| X-LISTENER | [smtp.bredband.net] |
| X-IronPort-Anti-Spam-Filtered | true |
| X-IronPort-Anti-Spam-Result | AmIbAAwHLVDVcDLgPGdsb2JhbAANOItfrlwBAQEBN4JUAQEBfBwYCRYRBwMCAQIBDyIGASAGAgEBh3oDpwCKHg2JToolZIZXA45agSCEAoxfh2c |
| X-IronPort-AV | E=Sophos;i="4.77,779,1336341600"; d="py'?scan'208,217";a="392097251" |
| Date | Thu, 16 Aug 2012 16:31:47 +0200 |
| From | Virgil Stokes <vs@it.uu.se> |
| User-Agent | Mozilla/5.0 (Windows NT 6.0; rv:15.0) Gecko/20120808 Thunderbird/15.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Strange behavior |
| Content-Type | multipart/mixed; boundary="------------040906040107050500000506" |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3364.1345128356.4697.python-list@python.org> (permalink) |
| Lines | 383 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1345128356 news.xs4all.nl 6935 [2001:888:2000:d::a6]:45412 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:27166 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On 16-Aug-2012 15:02, Peter Otten wrote:
> Virgil Stokes wrote:
>
>>>> def testFunc(startingList):
>>>> xOnlyList = []; j = -1
>>>> for xl in startingList:
>>>> if (xl[0] == 'x'):
>>> That's going to fail in the starting list contains an empty string. Use
>>> xl.startswith('x') instead.
>> Yes, but this was by design (tacitly assumed that startingList was both a
>> list and non-empty).
> You missunderstood it will fail if the list contains an empty string, not if
> the list itself is empty:
>
>>>> words = ["alpha", "", "xgamma"]
>>>> [word for word in words if word[0] == "x"]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> IndexError: string index out of range
>
> The startswith() version:
>
>>>> [word for word in words if word.startswith("x")]
> ['xgamma']
>
> Also possible:
>
>>>> [word for word in words if word[:1] == "x"]
> ['xgamma']
>
>> 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
> 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
>
>
Very nice Peter,
Here are the new results for timing with your method added (algorithm-3).
Method
average (sd) time in seconds
algorithm-1 (list comprehension)
0.11774 (0.002968)
algorithm-2 (S. D'Aprano)
0.17573 (0.003385)
algorithm-2A (modified S. D'Aprano)
0.18116 (0.003081)
algorithm-3 (improved list comprehension)
0.06639 (0.001728)
Algorithm-3 is 43% faster than algorithm-1. Again, the code used to obtain
these results is attached.
Thanks Peter for your contribution
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Strange behavior Virgil Stokes <vs@it.uu.se> - 2012-08-16 16:31 +0200
csiph-web