From: knifenomad on 19 Apr 2010 21:00 i know it's not very hard to get that solution. just by implementing simple function like below. def partition(target, predicate): """ split a list into two partitions with a predicate provided. any better ideas? :) """ true = [] false= [] for item in target: if predicates(item): true.append(item) else: false.append(item) return true, false but i wonder if there's another way to do this with standard libraries or .. built-ins. if it's not, i'd like the list objects to have partition method like string module has. true, false = [1,2,3,4].partition(lambda x: x >1) print true, false [2,3,4] [1]
From: Chris Rebert on 19 Apr 2010 21:16 On Mon, Apr 19, 2010 at 6:00 PM, knifenomad <knifenomad(a)gmail.com> wrote: > i know it's not very hard to get that solution. > just by implementing simple function like below. > > Â Â Â def partition(target, predicate): > Â Â Â Â Â Â """ > Â Â Â Â Â Â split a list into two partitions with a predicate > provided. > Â Â Â Â Â Â any better ideas? :) > Â Â Â Â Â Â """ > Â Â Â Â Â Â true = [] > Â Â Â Â Â Â false= [] > Â Â Â Â Â Â for item in target: > Â Â Â Â Â Â Â Â if predicates(item): > Â Â Â Â Â Â Â Â Â Â true..append(item) > Â Â Â Â Â Â Â Â else: > Â Â Â Â Â Â Â Â Â Â false.append(item) > Â Â Â Â Â Â return true, false > > but i wonder if there's another way to do this with standard libraries > or .. built-ins. > if it's not, i'd like the list objects to have partition method like > string module has. (A) str.partition() has a /completely/ different meaning from your partition() (B) You'd probably have better luck getting it added to the itertools module since the concept is applicable to all iterables. [http://docs.python.org/library/itertools.html] Cheers, Chris -- http://blog.rebertia.com
From: segunai on 19 Apr 2010 21:40 On 4ì20ì¼, ì¤ì 10ì16ë¶, Chris Rebert <c...(a)rebertia.com> wrote: > On Mon, Apr 19, 2010 at 6:00 PM, knifenomad <knifeno...(a)gmail.com> wrote: > > i know it's not very hard to get that solution. > > just by implementing simple function like below. > > >    def partition(target, predicate): > >       """ > >       split a list into two partitions with a predicate > > provided. > >       any better ideas? :) > >       """ > >       true = [] > >       false= [] > >       for item in target: > >         if predicates(item): > >           true.append(item) > >         else: > >           false.append(item) > >       return true, false > > > but i wonder if there's another way to do this with standard libraries > > or .. built-ins. > > if it's not, i'd like the list objects to have partition method like > > string module has. > > (A) str.partition() has a /completely/ different meaning from your partition() > (B) You'd probably have better luck getting it added to the itertools > module since the concept is applicable to all iterables. > [http://docs.python.org/library/itertools.html] > > Cheers, > Chris > --http://blog.rebertia.com yep, my mistake. i shouldn't have compared it to string's partition(). i just wanted that convenience string.partition() has as a built-in. anyway, thanks for itertools. :)
|
Pages: 1 Prev: ANN: Oktest 0.2.1 released - a new style testing library. Next: Carol Newman |