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.Fri, 17 Sep 2010 07:17:16 +0200sub list like subsetshttps://ask.sagemath.org/question/7693/sub-list-like-subsets/how to take lists of size from a list like subsets of size
example L = ([1,2,3,4],[3,5,6,7],[7,8,9,10])
From the above list find sublists of size 2
([1,2,3,4],[3,5,6,7])
([1,2,3,4],[7,8,9,10])
([3,5,6,7],[7,8,9,10])
subsets can also be size 3,4,5 as specifiedFri, 17 Sep 2010 06:08:08 +0200https://ask.sagemath.org/question/7693/sub-list-like-subsets/Answer by jsrn for <p>how to take lists of size from a list like subsets of size</p>
<p>example L = ([1,2,3,4],[3,5,6,7],[7,8,9,10])
From the above list find sublists of size 2</p>
<p>([1,2,3,4],[3,5,6,7])
([1,2,3,4],[7,8,9,10])
([3,5,6,7],[7,8,9,10])</p>
<p>subsets can also be size 3,4,5 as specified</p>
https://ask.sagemath.org/question/7693/sub-list-like-subsets/?answer=11658#post-id-11658I'm not completely sure I understand your question, but what you're asking for sounds simply (at least almost simply) like cartesian product, which is implemented in Python in itertools.product. In the basic case, you could then just do:
sage: L = [ [1,2,3,4],[3,5,6,7],[7,8,9,10] ] # note that this is a list, while you did a tuple
sage: from itertools import product
sage: list(product(L, L))
[([1, 2, 3, 4], [1, 2, 3, 4]), ([1, 2, 3, 4], [3, 5, 6, 7]), ...
(note the "list" around product, as product returns a generator)
The above can easily be generalised to having 3,4,...,n elements in each sublist (i.e. L^n) by modifying the last line to
list(product(repeat(L, n)))
However, this would also give those sublists where the same element might be present more than once in each list. If you don't want this, you can write a simple list-comprehension to handle the 2-case:
sage: [ [a,b] for a in L for b in L if a != b ]
[[[1, 2, 3, 4], [3, 5, 6, 7]], [[1, 2, 3, 4], [7, 8, 9, 10]], [[3, 5, 6, 7], [1, 2, 3, 4]], [[3, 5, 6, 7], [7, 8, 9, 10]], [[7, 8, 9, 10], [1, 2, 3, 4]], [[7, 8, 9, 10], [3, 5, 6, 7]]]
Manually, the above can also be extended to 3, 4, ... but not easily to any number n given as a parameter. Also, this has the drawback of creating the entire list in memory instead of being a light generator as the product returns. You would have to write a longer piece of code to get a generator with the above behaviour.
Note that this is all Python-stuff and not specific to Sage at all.
Cheers, JohanFri, 17 Sep 2010 06:32:42 +0200https://ask.sagemath.org/question/7693/sub-list-like-subsets/?answer=11658#post-id-11658Comment by sriram for <p>I'm not completely sure I understand your question, but what you're asking for sounds simply (at least almost simply) like cartesian product, which is implemented in Python in itertools.product. In the basic case, you could then just do:</p>
<pre><code>sage: L = [ [1,2,3,4],[3,5,6,7],[7,8,9,10] ] # note that this is a list, while you did a tuple
sage: from itertools import product
sage: list(product(L, L))
[([1, 2, 3, 4], [1, 2, 3, 4]), ([1, 2, 3, 4], [3, 5, 6, 7]), ...
</code></pre>
<p>(note the "list" around product, as product returns a generator)</p>
<p>The above can easily be generalised to having 3,4,...,n elements in each sublist (i.e. L^n) by modifying the last line to</p>
<pre><code>list(product(repeat(L, n)))
</code></pre>
<p>However, this would also give those sublists where the same element might be present more than once in each list. If you don't want this, you can write a simple list-comprehension to handle the 2-case:</p>
<pre><code>sage: [ [a,b] for a in L for b in L if a != b ]
[[[1, 2, 3, 4], [3, 5, 6, 7]], [[1, 2, 3, 4], [7, 8, 9, 10]], [[3, 5, 6, 7], [1, 2, 3, 4]], [[3, 5, 6, 7], [7, 8, 9, 10]], [[7, 8, 9, 10], [1, 2, 3, 4]], [[7, 8, 9, 10], [3, 5, 6, 7]]]
</code></pre>
<p>Manually, the above can also be extended to 3, 4, ... but not easily to any number n given as a parameter. Also, this has the drawback of creating the entire list in memory instead of being a light generator as the product returns. You would have to write a longer piece of code to get a generator with the above behaviour.</p>
<p>Note that this is all Python-stuff and not specific to Sage at all.</p>
<p>Cheers, Johan</p>
https://ask.sagemath.org/question/7693/sub-list-like-subsets/?comment=22642#post-id-22642find all sublists of set of a specified size say 3,4,5... from a given given list of setsFri, 17 Sep 2010 06:46:00 +0200https://ask.sagemath.org/question/7693/sub-list-like-subsets/?comment=22642#post-id-22642Comment by Mitesh Patel for <p>I'm not completely sure I understand your question, but what you're asking for sounds simply (at least almost simply) like cartesian product, which is implemented in Python in itertools.product. In the basic case, you could then just do:</p>
<pre><code>sage: L = [ [1,2,3,4],[3,5,6,7],[7,8,9,10] ] # note that this is a list, while you did a tuple
sage: from itertools import product
sage: list(product(L, L))
[([1, 2, 3, 4], [1, 2, 3, 4]), ([1, 2, 3, 4], [3, 5, 6, 7]), ...
</code></pre>
<p>(note the "list" around product, as product returns a generator)</p>
<p>The above can easily be generalised to having 3,4,...,n elements in each sublist (i.e. L^n) by modifying the last line to</p>
<pre><code>list(product(repeat(L, n)))
</code></pre>
<p>However, this would also give those sublists where the same element might be present more than once in each list. If you don't want this, you can write a simple list-comprehension to handle the 2-case:</p>
<pre><code>sage: [ [a,b] for a in L for b in L if a != b ]
[[[1, 2, 3, 4], [3, 5, 6, 7]], [[1, 2, 3, 4], [7, 8, 9, 10]], [[3, 5, 6, 7], [1, 2, 3, 4]], [[3, 5, 6, 7], [7, 8, 9, 10]], [[7, 8, 9, 10], [1, 2, 3, 4]], [[7, 8, 9, 10], [3, 5, 6, 7]]]
</code></pre>
<p>Manually, the above can also be extended to 3, 4, ... but not easily to any number n given as a parameter. Also, this has the drawback of creating the entire list in memory instead of being a light generator as the product returns. You would have to write a longer piece of code to get a generator with the above behaviour.</p>
<p>Note that this is all Python-stuff and not specific to Sage at all.</p>
<p>Cheers, Johan</p>
https://ask.sagemath.org/question/7693/sub-list-like-subsets/?comment=22641#post-id-22641You could also try, e.g., 'combinations([[1,2], [3,4,5], [6], [7,8,9]], 3)'. (See 'combinations?' for caveats.) There's also 'combinations_iterator'.Fri, 17 Sep 2010 07:17:16 +0200https://ask.sagemath.org/question/7693/sub-list-like-subsets/?comment=22641#post-id-22641