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.Tue, 01 Sep 2020 16:17:47 +0200Iteration over a Combinations(alist, 5).list()https://ask.sagemath.org/question/53220/iteration-over-a-combinationsalist-5list/ Dear all,
I have a big set built as : Combinations([1,2,3,4,5,6,7], 5).list() through which I need to iterate. In real life, the list [1,2,3,4,5,6,7] has 72 components. I don't need this set of combinations, only to iterate through it.
I did not find the proper way to do that with the combinat package, but I have the feeling I overlooked something.
Many thanks if you have infos on this issue!
Best, O. Sun, 30 Aug 2020 15:52:38 +0200https://ask.sagemath.org/question/53220/iteration-over-a-combinationsalist-5list/Answer by FrédéricC for <p>Dear all,</p>
<p>I have a big set built as : Combinations([1,2,3,4,5,6,7], 5).list() through which I need to iterate. In real life, the list [1,2,3,4,5,6,7] has 72 components. I don't need this set of combinations, only to iterate through it.
I did not find the proper way to do that with the combinat package, but I have the feeling I overlooked something. </p>
<p>Many thanks if you have infos on this issue!</p>
<p>Best, O. </p>
https://ask.sagemath.org/question/53220/iteration-over-a-combinationsalist-5list/?answer=53221#post-id-53221Ben, il suffit d'iterer :
sage: for c in Combinations([1,2,3,4,5,6,7], 5):
....: if 5 in c:
....: print(c)
....:
[1, 2, 3, 4, 5]
[1, 2, 3, 5, 6]
[1, 2, 3, 5, 7]
[1, 2, 4, 5, 6]
[1, 2, 4, 5, 7]
[1, 2, 5, 6, 7]
[1, 3, 4, 5, 6]
[1, 3, 4, 5, 7]
[1, 3, 5, 6, 7]
[1, 4, 5, 6, 7]
[2, 3, 4, 5, 6]
[2, 3, 4, 5, 7]
[2, 3, 5, 6, 7]
[2, 4, 5, 6, 7]
[3, 4, 5, 6, 7]
Sun, 30 Aug 2020 19:19:38 +0200https://ask.sagemath.org/question/53220/iteration-over-a-combinationsalist-5list/?answer=53221#post-id-53221Comment by Olivier R. for <p>Ben, il suffit d'iterer :</p>
<pre><code>sage: for c in Combinations([1,2,3,4,5,6,7], 5):
....: if 5 in c:
....: print(c)
....:
[1, 2, 3, 4, 5]
[1, 2, 3, 5, 6]
[1, 2, 3, 5, 7]
[1, 2, 4, 5, 6]
[1, 2, 4, 5, 7]
[1, 2, 5, 6, 7]
[1, 3, 4, 5, 6]
[1, 3, 4, 5, 7]
[1, 3, 5, 6, 7]
[1, 4, 5, 6, 7]
[2, 3, 4, 5, 6]
[2, 3, 4, 5, 7]
[2, 3, 5, 6, 7]
[2, 4, 5, 6, 7]
[3, 4, 5, 6, 7]
</code></pre>
https://ask.sagemath.org/question/53220/iteration-over-a-combinationsalist-5list/?comment=53243#post-id-53243Oh, merci (que je me sens cruche :) ! Mais soulagé !)Mon, 31 Aug 2020 23:40:43 +0200https://ask.sagemath.org/question/53220/iteration-over-a-combinationsalist-5list/?comment=53243#post-id-53243Comment by FrédéricC for <p>Ben, il suffit d'iterer :</p>
<pre><code>sage: for c in Combinations([1,2,3,4,5,6,7], 5):
....: if 5 in c:
....: print(c)
....:
[1, 2, 3, 4, 5]
[1, 2, 3, 5, 6]
[1, 2, 3, 5, 7]
[1, 2, 4, 5, 6]
[1, 2, 4, 5, 7]
[1, 2, 5, 6, 7]
[1, 3, 4, 5, 6]
[1, 3, 4, 5, 7]
[1, 3, 5, 6, 7]
[1, 4, 5, 6, 7]
[2, 3, 4, 5, 6]
[2, 3, 4, 5, 7]
[2, 3, 5, 6, 7]
[2, 4, 5, 6, 7]
[3, 4, 5, 6, 7]
</code></pre>
https://ask.sagemath.org/question/53220/iteration-over-a-combinationsalist-5list/?comment=53253#post-id-53253Bah, c'est seulement évident a posteriori. Ceci dit, cet iterateur est implementé recursivement, donc lent. `itertools.combinations` est sans doute plus rapide.Tue, 01 Sep 2020 16:17:47 +0200https://ask.sagemath.org/question/53220/iteration-over-a-combinationsalist-5list/?comment=53253#post-id-53253