给定字符串的Python排列

时间:2016-11-07 23:01:14

标签: python string permutation

我的代码非常简单。我的任务是得到给定字符串的每个排列。 计算排列数 - 这是明确的 - 因子。

代码

s = "aba"
perm = list("".join(string) for string in permutations(s))# all permutations
numberOfPerm = len(perm) #number of permutations
unique = len(list(set(perm))) #number of unique permutations
list.sort(perm) # ascii sorting
format_list = [numberOfPerm, unique]
print("total: {} (unique: {}) ".format(*format_list))
print(perm)

输出

total: 6 (unique: 3) 
['aab', 'aab', 'aba', 'aba', 'baa', 'baa']

事情是我需要它像这样

 total: 6 (unique: 3) aab, aab, aba, aba, baa, baa

我提出了各种解决方案,例如: ''.join(finalArray),但它们都不在我的pycharm或VPL(虚拟编程实验室)中工作 - 出现回溯错误。感谢最终的帮助。

1 个答案:

答案 0 :(得分:0)

print("total: {} (unique: {}) {}".format(*format_list, ', '.join(perm))