您的位置:首页 > 编程语言 > Python开发

python 实现全排列

2014-02-19 15:07 387 查看
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']
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: