Re: split up a list by condition?
- From: Reinhold Birkenfeld <reinhold-birkenfeld-nospam@xxxxxxxxxx>
- Date: Tue, 07 Jun 2005 21:19:23 +0200
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
.
- Follow-Ups:
- Re: split up a list by condition?
- From: Grooooops
- Re: split up a list by condition?
- References:
- split up a list by condition?
- From: Reinhold Birkenfeld
- Re: split up a list by condition?
- From: Raymond Hettinger
- split up a list by condition?
- Prev by Date: Re: DB API 2.0 and transactions
- Next by Date: Re: split up a list by condition?
- Previous by thread: Re: split up a list by condition?
- Next by thread: Re: split up a list by condition?
- Index(es):
Relevant Pages
|