【发布时间】:2017-08-10 17:51:57
【问题描述】:
问题: 如何指定我的 itertools.permutations 对象,以便它只包含列表中给出的一个开头的排列? (Python 3.5)
故事: 我有一个谜题,有 36 个谜题元素,所以理论上有 4e+41 种可能性。如果我使用 itertools.permutations 进行检查,它将永远计算。因此,我尝试检查哪些块适合放在第一位,然后只寻找第二位和第一名,这是可能的。
例如块 2 不适合放在首位,那么 itertools.permutations(mylist,2) 不应再包含 (2,1)(2,2)(2,3) 等组合。
mylist = [0,1,2,3] # ..,34,35 - contains all puzzle blocks
begin = [(1,2),(1,3)] # this is the list of possible first blocks, so itertools should only give me combinations, that begin with one of this possibillities
combs = itertools.permutations(mylist,3) # --- THIS IS THE LINE OF THE QUESTION ---
目前,我使用:
for thisposs in combs:
if thisposs[0:2] in begin:
checkblock(thisposs) # check if thisposs is a combination that solves the puzzle
但是这样 for 循环仍然会爆炸。我想指定 itertools 对象,以便它没有所有可能性,只有那些以数组/元组/列表中的内容开头的可能性:
begin = [(1,2),(1,3)] # only one of these should be allowed in first two slots
每次我增加一个准确度步长时,都会重新计算这个数组,所以在优化过程中,它可能会采用如下值:
begin = [1,3,4,5,6] # after permutations(mylist,1) - still a lot beginnings are possible
begin = [(1,6),(1,7),(5,1),(6,7)] # after permutations(mylist,2) - some combinations didn't work further
begin = [(5,1,2),(5,1,6),(5,1,7)] # after permutations(mylist,3) - the solution can for sure only begin with 5-2-???
# and so on
每次我做了一个“准确步骤”,我都会用一个新的 itertools 对象
combs = itertools.permutations(mylist,n+1)
所以我的问题是:如何指定我的 itertools.permutations 对象,以便它只包含列表中给出的一个开头的排列?
【问题讨论】:
-
不要使用
itertools.permutations。自己实现回溯搜索。 -
是的,这绝对没有内置支持。
标签: python algorithm performance optimization itertools