I am trying to create a series of limited permutations of a list of items. Each element has a category, and I need to find combinations of elements so that each combination does not have several elements from the same category. To illustrate a few sample data here:
Name | Category ==========|========== 1. Orange | fruit 2. Apple | fruit 3. GI-Joe | toy 4. VCR | electronics 5. Racquet | sporting goods
Combinations will be limited to three, I do not need every combination of each length. Thus, the set of combinations for the above list can be:
(Orange, GI-Joe, VCR) (Orange, GI-Joe, Racquet) (Orange, VCR, Racquet) (Apple, GI-Joe, VCR) (Apple, GI-Joe, Racquet) ... and so on.
I do this quite often, on different lists. Lists will not contain more than 40 items, but it is clear that they can create thousands of combinations (although each list will probably have about 10 unique categories, which somewhat limits them)
I came up with some pseudo python for how I will implement it recursively. It was too long since I took combinatorics, but from what I remember, it is, in fact, a subset of the combination of the set, something like C (list length, desired size). Probably some library modules that can make this cleaner (or at least more efficient)
I was wondering if it is possible that there is a better approach than what I have (maybe one that uses itertools.combinations ):