Q:

Get all subsets of a Python dictionary with restrictions

I have the following dictionary:
intervals = {'param1': [0, 1],
             'param2_hi': [4, 5, 6, 7, 8, 9],
             'param2_lo': [0, 1, 2, 3, 4, 5],
             'param3_hi': [9, 10, 11, 12, 13, 14, 15],
             'param3_lo': [5, 6, 7, 8, 9, 10],
             'param4': [0, 1],
             'param5_hi': [4, 5, 6, 7, 8, 9],
             'param5_lo': [0, 1, 2, 3, 4, 5]}

How would I create all possible subsets of this dictionary where I must have exactly one of each number x for 'paramx'?  Meaning that I can only have one entry for param1 (_lo or _hi), one entry for param2 (_lo or _hi), and so on, and that each subset must include every paramx that does not have a _lo or _hi. 
Here are two possible subsets:
subset_one = {'param1': [0, 1],
              'param2_hi': [4, 5, 6, 7, 8, 9],
              'param3_hi': [9, 10, 11, 12, 13, 14, 15],
              'param4': [0, 1],
              'param5_hi': [4, 5, 6, 7, 8, 9]}

subset_two = {'param1': [0, 1],
              'param2_lo': [0, 1, 2, 3, 4, 5],
              'param3_lo': [5, 6, 7, 8, 9, 10],
              'param4': [0, 1],
              'param5_lo': [0, 1, 2, 3, 4, 5]}

# Example of mixed '_hi' and '_lo' intervals
subset_three = {'param1': [0, 1],
                'param2_lo': [0, 1, 2, 3, 4, 5],
                'param3_hi': [9, 10, 11, 12, 13, 14, 15],
                'param4': [0, 1],
                'param5_lo': [0, 1, 2, 3, 4, 5]}

...

Note: I want to preserve key, value pairs.  
Edit: Added subset_three to show the possibility of having mixed '_lo' and '_hi' intervals. 

A:

I think both collections.defaultdict and itertools.product will be useful here.  First, construct a pool that sorts the keys based off what they start with.  Then, construct the Cartesian product of these bins. 
from collections import defaultdict
from itertools import product

pool = defaultdict(list)
for key in intervals:
    base = key.split('_')[0]
    pool[base].append(key)

subsets = [{key: intervals[key] for key in keys} for keys in product(*pool.values())]

# {'param2_lo': [0, 1, 2, 3, 4, 5], ... , 'param1': [0, 1]}
# {'param5_lo': [0, 1, 2, 3, 4, 5], ..., 'param2_hi': [4, 5, 6, 7, 8, 9]}
# and so on...

