2016-11-07 83 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