Python 给定字符串的排列
Python permutations of given string
我的代码很简单。我的任务是获取给定字符串的每个排列。
计算排列数 - 很清楚 - 阶乘。
代码
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)
,但其中 none 在我的 pycharm 或 VPL(虚拟编程实验室)中工作 - 出现回溯错误。感谢最终的帮助。
print("total: {} (unique: {}) {}".format(*format_list, ', '.join(perm))
我的代码很简单。我的任务是获取给定字符串的每个排列。 计算排列数 - 很清楚 - 阶乘。
代码
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)
,但其中 none 在我的 pycharm 或 VPL(虚拟编程实验室)中工作 - 出现回溯错误。感谢最终的帮助。
print("total: {} (unique: {}) {}".format(*format_list, ', '.join(perm))