给定一个字符串是否有任何简短的方法在Python 3中找到该字符串中空格分隔单词的不同组合?
例如:
如果输入字符串是'Peaches Apples Bananas',我想输出为:
'Peaches Apples Bananas'
'桃子香蕉苹果'
'苹果香蕉桃子'
'苹果桃子香蕉'
'Bananas Peaches Apples'
'Bananas Apples Peaches'
答案 0 :(得分:1)
我认为您正在寻找itertools.permutations
:
import itertools
for perm in itertools.permutations('Peaches Apples Bananas'.split(' ')):
print(' '.join(perm))
答案 1 :(得分:1)
import itertools
string = 'Peaches Apples Bananas'
word_list = string.split(' ')
output = [' '.join(permutation) for permutation in itertools.permutations(word_list)]