Python 3如何查找字符串的不同组合 [英] Python 3 How to find the different combinations of a String

查看:49
本文介绍了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如何查找字符串的不同组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆