生成列表中字符串的组合[Python3]

时间:2015-08-01 20:17:58

标签: python python-3.x

我有一个字符串"dog",我不想用该字母的字母生成所有可能的组合。

输出将是这样的:

["dog","dgo","ogd","odg","god","gdo"]

订单根本不重要。

1 个答案:

答案 0 :(得分:2)

使用itertools.permutations

import itertools

word = 'dog'
result = [''.join(new_word) for new_word in itertools.permutations(word)]

print(result)