查找所有可能的组合 [英] Find all possible combinations

查看:73
本文介绍了查找所有可能的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我早些时候问过这个问题,但是关于另一种编程语言.

I asked this question earlier but regarding another programming languages.

假设我有几个根,前缀和后缀.

Let's say I have a couple roots, prefixes, and suffixes.

roots = ["car insurance", "auto insurance"]
prefix = ["cheap", "budget"]
suffix = ["quote", "quotes"]

Python中有一个简单的函数,可以让我构造三个字符向量的所有可能组合.

Is there a simple function in Python which will allow me to construct all possible combinations of the three character vectors.

所以我想要一个列表或其他数据结构,该列表或其他数据结构返回以下每个字符串的所有可能组合的列表.

So I want a list or other data structures which returns the following list of all possible combinations of each string.

cheap car insurance quotes
cheap car insurance quotes
budget auto insurance quotes
budget insurance quotes
...

推荐答案

使用 itertools.product() :

for p, r, s in itertools.product(prefix, roots, suffix):
    print p, r, s

这篇关于查找所有可能的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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