我的代码非常简单。我的任务是获得给定字符串的每个排列。 计算排列数 - 这是明确的 - 阶乘。给定字符串的Python排列
代码
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(虚拟编程实验室)中工作 - 出现回溯错误。感谢您的帮助。
难道你不认为这将有助于*后您收到的错误?* [在列表中的字符串拼接项目(的 –
可能的复制http://stackoverflow.com/questions/12453580/串联项目列表到字符串) –