<p>如果我正确地理解了您的问题,这应该符合您的要求:</p>
<pre><code>from copy import deepcopy
# slice into appropriate chunks
def slice_lst(size_tup, lst):
for index, length in enumerate(size_tup):
running_total = sum(size_tup[:index])
yield lst[running_total:running_total+length]
# perform integer partition
def integer_partition(num):
return {(x,) + y for x in range(1, num) for y in integer_partition(num-x)} | {(num,)}
# create all partitions given the starting list
def subsets(lst):
for partition in integer_partition(len(lst)):
yield list(slice_lst(partition, deepcopy(lst)))
# check that 1,2 and 3 are always contained within the same subset
def triplet_case(lst):
bool_arr = [1 in lst, 2 in lst, 3 in lst]
return all(bool_arr) or not any (bool_arr)
# check that 7 and 8 are always contained within the same subset
def duplet_case(lst):
bool_arr = [7 in lst, 8 in lst]
return all(bool_arr) or not any(bool_arr)
for subset in subsets([1,2,3,4,5,6,7,8,9]):
if all([triplet_case(s) for s in subset]) and all([duplet_case(s) for s in subset]):
print subset
</code></pre>
<p>如果您有任何后续问题,请随时提出!在</p>