Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27159

Re: Strange behavior

From Peter Otten <__peter__@web.de>
Subject Re: Strange behavior
Date 2012-08-16 15:02 +0200
Organization None
References <1a1834ae-2b4a-473f-b626-f37a17588199@googlegroups.com> <mailman.3285.1344973216.4697.python-list@python.org> <502aeb2b$0$29978$c3e8da3$5496439d@news.astraweb.com> <502CD723.5090201@it.uu.se>
Newsgroups comp.lang.python
Message-ID <mailman.3359.1345122171.4697.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Strange behavior light1quark@gmail.com - 2012-08-14 08:38 -0700
  Re: Strange behavior Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-08-14 17:59 +0200
    Re: Strange behavior Terry Reedy <tjreedy@udel.edu> - 2012-08-14 15:05 -0400
  Re: Strange behavior Virgil Stokes <vs@it.uu.se> - 2012-08-14 21:40 +0200
    Re: Strange behavior Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-15 00:19 +0000
      Re: Strange behavior Virgil Stokes <vs@it.uu.se> - 2012-08-16 13:18 +0200
        Re: Strange behavior Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 17:40 +0000
      Re: Strange behavior Peter Otten <__peter__@web.de> - 2012-08-16 15:02 +0200
  Re: Strange behavior light1quark@gmail.com - 2012-08-14 12:20 -0700
    Re: Strange behavior Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-08-15 11:57 +0200
  Re: Strange behavior Chris Angelico <rosuav@gmail.com> - 2012-08-15 07:55 +1000
    Re: Strange behavior Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-08-15 11:50 +0200

csiph-web