我的代碼非常簡單。我的任務是獲得給定字符串的每個排列。 計算排列數 - 這是明確的 - 階乘。給定字符串的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/串聯項目列表到字符串) –