多亏了jornsharpe给我的links to itertools响应我的previous attempt,我有了一个新的策略,那就是调整那些函数以符合我的需求(因此是“dummy”和更长的计数器名等等)。不幸的是,我现在得到了这样的结果(包括Spyder和我需要提交的基于web的提交格式):
[<generator object combinations_of_options>]
而不是实际值。我不知所措。如何取回实际结果而不是指向结果的指针?你知道吗
def from_list(some_list):
'''turns an interable into individual elements'''
for dummy in some_list:
for element in dummy:
yield element
def combinations_of_options(options, length):
''' yields all combinations of option in specific length
'''
pool = tuple(options)
pool_len = len(pool)
if length > pool_len:
return
indices = range(length)
yield tuple(pool[index] for index in indices)
while True:
for index in reversed(range(length)):
if indices[index] != index + pool_len - length:
break
else:
return
indices[index] += 1
for dummy_index in range(index+1, length):
indices[dummy_index] = indices[dummy_index-1] + 1
yield tuple(pool[index] for index in indices)
def gen_proper_subsets(outcomes):
outcomes = list(outcomes)
max_len = len(outcomes)
values = [combinations_of_options(outcomes, max_len) for dummy in range(max_len+1)]
print values
return from_list(values)
需要输入/输出: 在(4,2,2)中 输出(4,2,2),(4,2),(2,2),(4,),(2,),()
在(2,4,2) 输出(2,4,2),(2,4),(4,2),(2,2),(4,),(2,),()
调用
combinations_of_outcomes()
函数确实会返回一个生成器,您必须对其进行迭代以提取值。也许不是你可以试试
看起来好像当前
max_len
的值为零,所以在结果中只能看到一个生成器。更改后,您将看到包含列表的一个列表元素。你知道吗相关问题 更多 >
编程相关推荐