Re: split up a list by condition?



Raymond Hettinger wrote:
>>> while writing my solution for "The python way?", I came across this fragment:
>>> vees = [c for c in wlist[::-1] if c in vocals]
>>> cons = [c for c in wlist[::-1] if c not in vocals]
>>>
>>> So I think: Have I overlooked a function which splits up a sequence
>>> into two, based on a condition
>
> Trying to compress too much into one line is not "the python way" ;-)

I know (there is a Guido quote about this, I just lost the source...)

> vees, cons = [], []
> for c in reversed(wlist):
> if c in vocals:
> vees.append(c)
> else:
> cons.append(c)
>

Well, I've found a uglier solution,

vees, cons = [], []
[(vees, cons)[ch in vocals].append(ch) for ch in wlist]

Reinhold
.



Relevant Pages

  • Re: how to get the file name
    ... see it in the filesystem, so whatever changes I make to ... the file will be lost as soon as I close it. ... It's a different file with a different inode. ... Prev by Date: ...
    (comp.os.linux.development.apps)
  • RE: Word for windows dropped off my pc suddenly, how do I get it back?
    ... I dont want to have to pay for a new download when I ... > already have it but just seemed to have lost it entirely in my computer? ... Prev by Date: ...
    (microsoft.public.word.docmanagement)
  • Creating an object during runtime
    ... poor coding structure / lack of comments, but I'm am a bit lost on how to ... Prev by Date: ...
    (comp.lang.java.programmer)
  • Re: uploading forms on web pages
    ... > I have created my first form using publisher 2000. ... > I am lost. ... The form is located under the alumni ... Prev by Date: ...
    (microsoft.public.publisher)
  • Re: How do I send an attachment From My Documents?
    ... Hint: It's not Word. ... > lost and confused wrote: ... >>>Did you have a question about Microsoft Word? ... Prev by Date: ...
    (microsoft.public.word.docmanagement)