any modules having a function to partition a list by predicateprovided?

K

knifenomad

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]
 
C

Chris Rebert

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
 
S

segunai

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. :)
 
K

Krister Svanlund

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. :)

The way I would do it is probably apply filter to the list:[5, 7, 11, 13, 17, 19, 23]
 
B

Bryan

knifenomad said:
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


I've oft collected partitions in a dict, so I'll suggest the
generalization:

def partition(target, predicate):
result = {}
for item in target:
result.setdefault(predicate(item), []).append(item)
return result

true, false = [1,2,3,4].partition(lambda x: x >1)

print true, false
[2,3,4] [1]

With my version you'd get:
{False: [1], True: [2, 3, 4]}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,797
Messages
2,569,647
Members
45,376
Latest member
Coy6815793

Latest Threads

Top