我处理任意维的多维结构。我有一个xrange
迭代器的Python列表,每个迭代器代表多维数组的一个索引:
indices = [ i, j, k ]
在哪里
^{pr2}$为了生成所有可能的值,我使用以下朴素的递归代码:
def travtree(index,depth):
"Recursion through index list"
if depth >= len(indices):
# Stopping Condition
print index
else:
# Recursion
currindexrange = indices[depth]
for currindex in xrange(len(currindexrange)):
newindex = list(index) # list copy
newindex.append(currindexrange[currindex])
travtree(newindex,depth+1)
travtree([],0)
这很好,但我想知道,有没有更有效的,Python式的方法?我试着在itertools
模块中查找,但没有什么东西能跳出来。
相关问题 更多 >
编程相关推荐