python 实现全排列

来源:互联网 发布:美工考核试题 编辑:程序博客网 时间:2024/05/03 03:21
def permutation(result, str, list):    """        取一个数组的全排列        list:为输入列表        str:传空字符串        result: 为结果列表    """    if len(list) == 1:        result.append(str + "," + list[0])    else:        for temp_str in list:            temp_list = list[:]            temp_list.remove(temp_str)            permutation(result, str + "," + temp_str, temp_list)


测试调用

    test = []    permutation(test,"",['cn_444934666','cn_363488188','cn_414478124'])    print test

输出:

[',cn_444934666,cn_363488188,cn_414478124', ',cn_444934666,cn_414478124,cn_363488188', ',cn_363488188,cn_444934666,cn_414478124', ',cn_363488188,cn_414478124,cn_444934666', ',cn_414478124,cn_444934666,cn_363488188', ',cn_414478124,cn_363488188,cn_444934666']

0 0
原创粉丝点击