mercredi 6 juillet 2016

fastest way in python to enumerate all combinations and return the index


It is hard to describe my problem in the title. I think the title of this problem is not very good. My problem is the following

Say I have a list [0,0,0,0,0], I want to put 3 ones into these 5 slots in the list. I want to enumerate all the possible combinations. In this case, it will be 5 choose 3, which is 10, e.g.,

[1,1,1,0,0]
[1,0,1,0,1]
....

I wish eventually, to get a list of lists such that each element (which is still a list) in the big list, stores the index of those ones in each scenario, e.g., in the example above, the 1st element in the final big list should be [0,1,2], the 2nd element in the final big list should be [0,2,4]...

Is there a fast way of achieving this? I guess I need to use the library itertools, but not sure which specific function should I use


Aucun commentaire:

Enregistrer un commentaire