本文介绍了Python 3如何查找字符串的不同组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出一个字符串,Python 3中没有什么捷径可以找到该字符串中以空格分隔的单词的不同组合?
Given a String is there any short way in Python 3 to find the different combinations of the space seperated words in that string ?
例如:
如果输入字符串是"Peaches Apples Bananas",我希望输出为:
If the input string is 'Peaches Apples Bananas', I want output as:
桃子苹果香蕉"
桃子香蕉苹果"
苹果香蕉桃子"
苹果桃子香蕉"
香蕉桃子苹果"
香蕉苹果桃子"
推荐答案我认为您正在寻找 itertools.permutations :
I think you are looking for itertools.permutations:
import itertools for perm in itertools.permutations('Peaches Apples Bananas'.split(' ')): print(' '.join(perm))更多推荐
Python 3如何查找字符串的不同组合
发布评论