2016-11-07 82 views
-1

我的代码非常简单。我的任务是获得给定字符串的每个排列。 计算排列数 - 这是明确的 - 阶乘。给定字符串的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(虚拟编程实验室)中工作 - 出现回溯错误。感谢您的帮助。

+0

难道你不认为这将有助于*后您收到的错误?* [在列表中的字符串拼接项目(的 –

+0

可能的复制http://stackoverflow.com/questions/12453580/串联项目列表到字符串) –

回答

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

此解决方案不通过VPL,但绝对可用于pycharm。 文件“Anagram.py”,第10行 print(“total:{}(unique:{}){}”。格式(* format_list,','.join(perm))) SyntaxError:可能会遵循*表达式 – Rickertbrandsen

+0

尝试使用'format_list [0],format_list [1]' – Uriel

+0

更改'* format_list'好像是我对python基础知识的问题,另一种失败出现了。我正在使用's = str(input())'而不是's =“aba”'并且在这个测试中有两个失败 '追踪(最近呼叫最后): 文件“Ana.py”,线4,在 S = STR(输入()) 文件 “”,第1行,在 NameError:名称 '克拉德诺' 不是defined' – Rickertbrandsen