Ask Your Question
1

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

asked 2020-08-30 08:52:38 -0500

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 flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2020-08-30 12:19:38 -0500

FrédéricC gravatar image

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]
edit flag offensive delete link more

Comments

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

Olivier R. gravatar imageOlivier R. ( 2020-08-31 16:40:43 -0500 )edit

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

FrédéricC gravatar imageFrédéricC ( 2020-09-01 09:17:47 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-08-30 08:52:38 -0500

Seen: 58 times

Last updated: Aug 30