在使用itertools permutations函數後列表中存在一些問題。將itertools.permutations的輸出從元組列表轉換爲字符串列表
from itertools import permutations
def longestWord(letters):
combinations = list(permutations(letters))
for s in combinations:
''.join(s)
print(combinations)
longestWord("aah")
輸出看起來是這樣的:
[('a', 'a', 'h'), ('a', 'h', 'a'), ('a', 'a', 'h'), ('a', 'h', 'a'),
('h', 'a', 'a'), ('h', 'a', 'a')]
我想這是一個簡單的列表,但它似乎現身元組的列表(?)。任何人都可以幫我格式化它,所以它出現如下:
['aah', 'aha', 'aah', 'aha', 'haa', 'haa']
調用排列'combinations'是混亂的。 – DSM 2013-04-28 16:58:52