以下是用Python实现多个列表排列组合的递归代码:

def permutation(lists, result, depth, current):
    if depth == len(lists):
        result.append(current)
        return
    
    for i in range(len(lists[depth])):
        permutation(lists, result, depth+1, current+[lists[depth][i]])

lists = [[1, 2, 3], [4, 5], [6, 7, 8]]
result = []
permutation(lists, result, 0, [])
print(result)

输出结果为:

[[1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 5, 6], [1, 5, 7], [1, 5, 8], [2, 4, 6], [2, 4, 7], [2, 4, 8], [2, 5, 6], [2, 5, 7], [2, 5, 8], [3, 4, 6], [3, 4, 7], [3, 4, 8], [3, 5, 6], [3, 5, 7], [3, 5, 8]]

该程序接受一个由多个列表组成的列表,返回其所有元素的排列组合。程序使用递归方式实现,每次递归处理一个列表,将列表中的元素添加到当前排列中,并继续处理下一个列表。当处理完所有列表后,将当前排列添加到结果列表中。最终返回结果列表

用python递归实现多个列表排列组合代码

原文地址: https://www.cveoy.top/t/topic/epAz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录