ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 24 Mar 2013 19:50:23 +0100Partition list satisfying conditionshttps://ask.sagemath.org/question/9944/partition-list-satisfying-conditions/Hi, I am looking for a function which will partition a list. For a simple example, suppose I have the list `[1,2,3,4,5,6]`. Suppose I want all the even integers in one list and the odd integers in another. Is there a function that can do this, and perhaps yeild something like `[[1,3,5],[2,4,6]]`? In this case, the number of partitions is 2, so it is fairly easy, but in my problem the number is not known. In mathematica, the function I want is called GatherBy.Sun, 24 Mar 2013 18:35:39 +0100https://ask.sagemath.org/question/9944/partition-list-satisfying-conditions/Answer by fidbc for <p>Hi, I am looking for a function which will partition a list. For a simple example, suppose I have the list <code>[1,2,3,4,5,6]</code>. Suppose I want all the even integers in one list and the odd integers in another. Is there a function that can do this, and perhaps yeild something like <code>[[1,3,5],[2,4,6]]</code>? In this case, the number of partitions is 2, so it is fairly easy, but in my problem the number is not known. In mathematica, the function I want is called GatherBy.</p>
https://ask.sagemath.org/question/9944/partition-list-satisfying-conditions/?answer=14686#post-id-14686For the particular example you have you can try
sage: l=range(10)
sage: odd=filter(lambda x: x%2, l)
sage: even=filter(lambda x: x%2==0, l)
sage: odd
[1, 3, 5, 7, 9]
sage: even
[0, 2, 4, 6, 8]
Otherwise you can try implementing a naive version of `gather_by` using dictionaries as follows
def gather_by( l, f ):
parts = {}
for e in l:
cur_key = f(e)
if parts.has_key( cur_key ):
parts[cur_key].append( e )
else:
parts[cur_key] = [ e ]
return parts.values()
Then
sage: gather_by( range(10), lambda x: x%2)
[[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]
Sun, 24 Mar 2013 19:15:40 +0100https://ask.sagemath.org/question/9944/partition-list-satisfying-conditions/?answer=14686#post-id-14686Comment by sjcavazos2 for <p>For the particular example you have you can try</p>
<pre><code>sage: l=range(10)
sage: odd=filter(lambda x: x%2, l)
sage: even=filter(lambda x: x%2==0, l)
sage: odd
[1, 3, 5, 7, 9]
sage: even
[0, 2, 4, 6, 8]
</code></pre>
<p>Otherwise you can try implementing a naive version of <code>gather_by</code> using dictionaries as follows</p>
<pre><code>def gather_by( l, f ):
parts = {}
for e in l:
cur_key = f(e)
if parts.has_key( cur_key ):
parts[cur_key].append( e )
else:
parts[cur_key] = [ e ]
return parts.values()
</code></pre>
<p>Then</p>
<pre><code>sage: gather_by( range(10), lambda x: x%2)
[[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]
</code></pre>
https://ask.sagemath.org/question/9944/partition-list-satisfying-conditions/?comment=18023#post-id-18023Perfect! Thanks!Sun, 24 Mar 2013 19:50:23 +0100https://ask.sagemath.org/question/9944/partition-list-satisfying-conditions/?comment=18023#post-id-18023