2^n Itertools combinations with advanced filtering

Viewed 1258

I know that I can use itertools to pump out combinations, and define the size of the combination group, like so:

import itertools
print list(itertools.combinations(['V','M','T','O','Q','K','D','R'], 4))

The output of this would be like a list of tuples, each of length 4 in this case.

From here, what I'd like to do is enforce 2 parameters - 1)exclude any combinations/tuples that contain certain pairs - both V and M for instance, or Q and K. 2)Force each tuple to contain only 1 instance of a letter. I believe that itertools is already doing #2.

What should remain are only those tuple groups that do not contain any of these pre-determined "false" pairs. So if I excluded a group that contained V and M, the group ('V','M','Q','D') would be invalid, but ('V','R','Q','D') would be valid.

What's the best way for me to do this?

3 Answers
Related