从多个列表中选择组合 [英] pick combinations from multiple lists

查看:43
本文介绍了从多个列表中选择组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,我正在努力形成多个列表的组合.所以,我有三个(可能还有更多)看起来像这样:

uk_rock_stars=[1,2,3,4,5,6,7,8,9]uk_pop_stars=[10,11,12,13,1,4,6,22,81]us_stars=[22,34,44,7,33,99,22,77,99]..

具有相同长度的所有列表.现在,我想生成它们的组合列表,其中 N 是上面列表的总数.我正在寻找如下结果:

comb=[(1,10,22),(1,10,34),(1,10,44)...等(所有组合....]

这样,每个组合,比如 (1,10,22) 的长度与原始列表的数量相同(在这种情况下为 3)

解决方案

阅读此http://docs.python.org/2/library/itertools.html#itertools.product,它解释了一切.

itertools 是一个包,它具有一系列用于迭代集合的有用功能.一个有用的功能是 product 函数,它创建一个生成器,该生成器将迭代您提供的任意数量可迭代集合的笛卡尔积.

itertools.product 的结果不是一个列表,而是一个生成器.python 生成器类似于其他语言中的协程.这意味着它将根据需要计算您的组合.如果您计算每个大小为 100 的三个迭代器的乘积,但您只使用前 10 个左右,itertools.product 将只计算 10 个组合,而不是计算所有 100^3 个组合.>

如果你真的想要一个列表对象而不是一个生成器(也许你想计算切片或其他东西)调用 list 函数并将你的生成器对象作为参数传递.

以下代码生成所有组合并打印结果.

代码:

import itertoolsuk_rock_stars=[1,2,3,4,5,6,7,8,9]uk_pop_stars=[10,11,12,13,1,4,6,22,81]us_stars=[22,34,44,7,33,99,22,77,99]用于 itertools.product(uk_rock_stars, uk_pop_stars, us_stars) 中的组合:印刷组合

输出:

(1, 10, 22)(1, 10, 34)(1, 10, 44)(1, 10, 7)(1, 10, 33)(1, 10, 99)(1, 10, 22)(1, 10, 77)(1, 10, 99)(1, 11, 22)(1, 11, 34)(1, 11, 44)(1, 11, 7)(1, 11, 33)(1, 11, 99)(1, 11, 22)(1, 11, 77)(1, 11, 99)...等等.

I am new to python and I am struggling to form a combination of multiple lists. So, I have three (and possible more) looking like this:

uk_rock_stars=[1,2,3,4,5,6,7,8,9]
uk_pop_stars=[10,11,12,13,1,4,6,22,81]
us_stars=[22,34,44,7,33,99,22,77,99]
.
.

with all lists of the same length. Now, I would like to generate a combination list of them, with N being the total number of lists above. I am looking for a result looking like:

comb=[(1,10,22),(1,10,34),(1,10,44)...etc (all combinations)....]

such that, each combinations, say (1,10,22) is of same length as the number of original lists (in this case 3)

解决方案

Read over this http://docs.python.org/2/library/itertools.html#itertools.product, it explains everything.

itertools is a package that has a bunch of useful functionality for iterating over collections. One useful feature is the product function which creates a generator that will iterate over the cartesian product of any number of iterable collections you give it.

The result of itertools.product is not a list, it is a generator. A python generator is similar to a coroutine in other languages. This means it will compute your combinations on an as needed basis. If you compute the product of three iterables that each have size 100, but you only use the first 10 or so, itertools.product will only compute 10 combinations instead of computing all 100^3 combinations.

If you actually want a list object instead of a generator (maybe you want to compute slices or something) call the list function and pass your generator object as an argument.

The following code produces all combinations and prints the results.

Code:

import itertools

uk_rock_stars=[1,2,3,4,5,6,7,8,9]
uk_pop_stars=[10,11,12,13,1,4,6,22,81]
us_stars=[22,34,44,7,33,99,22,77,99]

for combination in itertools.product(uk_rock_stars, uk_pop_stars, us_stars):
    print combination

Outputs:

(1, 10, 22)
(1, 10, 34)
(1, 10, 44)
(1, 10, 7)
(1, 10, 33)
(1, 10, 99)
(1, 10, 22)
(1, 10, 77)
(1, 10, 99)
(1, 11, 22)
(1, 11, 34)
(1, 11, 44)
(1, 11, 7)
(1, 11, 33)
(1, 11, 99)
(1, 11, 22)
(1, 11, 77)
(1, 11, 99)
...
etc.

这篇关于从多个列表中选择组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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