Python 递归迭代器实现全排列算法

def permutations(lis,ind=0):
    # 保存原始列表
    old_lis = copy.deepcopy(lis)
    if ind == len(lis):
        yield old_lis
    for i in range(ind,len(lis)):
        # 交换
        lis[ind],lis[i] = lis[i],lis[ind]
        yield from permutations(lis,ind+1)
        # 恢复原来的列表
        lis = old_lis

lis = ["a", "b", "c", "d"]
for i in permutations(lis):
    print(i)

你可能感兴趣的:(python,java,前端)