不同列表之间的全排列

不同列表之间的全排列,适用于不同数据类型列表之间的组合

#coding='utf8'
board = {2: ['0', '1'], 3: ['0', '1', '2'], 4: ['0', '1', '2', '3'], 5: ['0', '1', '2', '3', '4'], 6: ['0', '1', '2', '3', '4', '5'], 7: ['0', '1', '2', '3', '4', '5', '6'], 8: ['0', '1', '2', '3', '4', '5', '6', '7'], 9: ['0', '1', '2', '3', '4', '5', '6', '7', '8'],10: ['0', '1', '2', '3', '4', '5', '6', '7', '8','9'],
        11: ['0', '1', '2', '3', '4', '5', '6', '7', '8','9','A'], 12: ['0', '1', '2', '3', '4', '5', '6', '7', '8','9','A','B'],13: ['0', '1', '2', '3', '4', '5', '6', '7', '8','9','A','B','C'],14: ['0', '1', '2', '3', '4', '5', '6', '7', '8','9','A','B','C','D'],15: ['0', '1', '2', '3', '4', '5', '6', '7', '8','9','A','B','C','D','E'],16: ['0', '1', '2', '3', '4', '5', '6', '7', '8','9','A','B','C','D','E','F']
         }
def combination(*args: list):
    config = [e for e in args]
    res = []
    tmp = []
    # 把整体的列表都放在config
    dfs(res, tmp, config, 0)
    return res

# dfs没变动
def dfs(res=[], tmp=[], config=[], i=0):
    if i == len(config):
        res.append(tmp.copy())
    else:
        for e in config[i]:
            # change spot
            tmp.append(e)
            # new condition,new recursion
            dfs(res, tmp, config, i + 1)
            # restore spot
            tmp.pop(len(tmp) - 1)

"""
combination用来对所有输入列表进行全排列
"""
a = [[1,2,3,4],['s','s','s','s'],[5,6,7,8,8],[1,2,3,4,5],[6,7,8,9]]
combination(*a)
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页