h1h*_*1h1 6 python string tuples list permutation
使用itertools置换函数后,列表中存在一些问题.
from itertools import permutations
def longestWord(letters):
combinations = list(permutations(letters))
for s in combinations:
''.join(s)
print(combinations)
longestWord("aah")
Run Code Online (Sandbox Code Playgroud)
输出如下所示:
[('a', 'a', 'h'), ('a', 'h', 'a'), ('a', 'a', 'h'), ('a', 'h', 'a'),
('h', 'a', 'a'), ('h', 'a', 'a')]
Run Code Online (Sandbox Code Playgroud)
我希望这是一个简单的列表,但它似乎是一个元组列表(?).任何人都可以帮助我格式化,所以它出现如下:
['aah', 'aha', 'aah', 'aha', 'haa', 'haa']
Run Code Online (Sandbox Code Playgroud)
from itertools import permutations
def longestWord(letters):
return [''.join(i) for i in permutations(letters)]
print(longestWord("aah"))
Run Code Online (Sandbox Code Playgroud)
结果:
['aah', 'aha', 'aah', 'aha', 'haa', 'haa']
Run Code Online (Sandbox Code Playgroud)
一些建议:
combination并不好,因为组合与排列不同