# Iteration over a Combinations(alist, 5).list()

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.

edit retag close merge delete

Sort by » oldest newest most voted

Ben, 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]

more

Oh, merci (que je me sens cruche :) ! Mais soulagé !)

( 2020-08-31 23:40:43 +0200 )edit
1

Bah, c'est seulement évident a posteriori. Ceci dit, cet iterateur est implementé recursivement, donc lent. itertools.combinations est sans doute plus rapide.

( 2020-09-01 16:17:47 +0200 )edit