-1

例如,我在使用递归查找组合时遇到了麻烦

a = ['a', 'b','c'], 
b = ['d','e','f']
c = ['g','h','i']

输入是 a,b,c 有 27 种可能的组合

combinations = []
if string == "":
    print (combinations)
    return
else:
    for i in string.head():
        recursive_combinations(combinations , string.tail())
4

1 回答 1

3

最简单、惯用的方法是使用itertools,而不是递归。像这样:

import itertools as it

v1 = ['a', 'b','c']
v2 = ['d','e','f']
v3 = ['g','h','i']

list(it.product(v1, v2, v3))
=> ... the cartesian product of the input lists ...

len(list(it.product(v1, v2, v3)))
=> 27
于 2013-09-16T15:38:05.267 回答