2个列表的Python组合 [英] Python combinations of 2 lists
问题描述
pythonic
计算两个列表的所有乘积组合的方式是什么.因此,给定两个长度为n
的列表,我想返回一个包含产品的长度为2^n
的列表.
What is the pythonic
way to calculated all the product combinations of two lists. So given two lists of length n
I'd liked to return a list of length 2^n
containing the products.
类似于list(itertools.product(on,off))
,但结果应使用所有四个元素,而不仅要使用组合对,例如:
Like list(itertools.product(on,off))
but the results should use all four elements not only combination pairs like:
[(1.05, 5.53), (1.05, 3.12), (1.05, 3.75), (1.05, 4.75), (1.5, 5.53), (1.5, 3.12), (1.5, 3.75), (1.5, 4.75), (2.1, 5.53), (2.1, 3.12), (2.1, 3.75), (2.1, 4.75), (1.7, 5.53), (1.7, 3.12), (1.7, 3.75), (1.7, 4.75)]
更像这样:
off = [5.53,3.12,3.75,4.75]
on = [1.05,1.5,2.1,1.7]
# calculate combinations
x = combinations(on,off)
# Where...
# x[0] = off[0] * off[1] * off[2] * off[3] i.e
# x[0] = 5.53 * 3.12 * 3.75 * 4.75
#
# x[1] = off[0] * off[1] * off[2] * on[3] i.e
# x[1] = 5.53 * 3.12 * 3.75 * 1.7
#
# x[2] = off[0] * off[1] * on[2] * on[3] i.e
# x[2] = 5.53 * 3.12 * 2.1 * 1.7
#
# ...
#
# x[15] = on[0] * on[1] * on[2] * on[3] i.e
# x[15] = 1.05 * 1.5 * 2.1 * 1.7
输出可能类似于itertools.product()
方法即 [(5.53, 3.12, 3.75, 4.75),(5.53, 3.12, 3.75, 1.7), ...]
.我需要计算乘积,但我对组合方法很感兴趣.
The output can be similar to the itertools.product()
method i.e [(5.53, 3.12, 3.75, 4.75),(5.53, 3.12, 3.75, 1.7), ...]
I need to calculate the product but I'm interesting in the combination method.
注意:当我说pythonic
的方法时,是指利用pythons结构(库(itertools ect))的一两行.
Note: When I say pythonic
way of doing this I mean a simple one or two lines taking advantage of pythons structures, libraries (itertools ect).
推荐答案
您可以从itertools.product([0, 1], 4)
开始生成所有2**4
可能性.枚举长度为4的0
和1
的所有可能序列,然后可以将每个0-1序列转换为off
和on
的值序列,如果0-1序列的第一个元素为0
,否则为on[i]
.在代码中:
You can generate all 2**4
possibilities by starting with itertools.product([0, 1], 4)
. This enumerates all possible sequences of 0
s and 1
s of length 4, and then you can translate each 0-1 sequence to a sequence of values from off
and on
, by taking off[i]
if the ith element of the 0-1 sequence is 0
, and on[i]
otherwise. In code:
>>> import itertools
>>> off = [5.53,3.12,3.75,4.75]
>>> on = [1.05,1.5,2.1,1.7]
>>> for choices in itertools.product([0, 1], repeat=len(off)):
... print [(on[i] if choice else off[i]) for i, choice in enumerate(choices)]
...
[5.53, 3.12, 3.75, 4.75]
[5.53, 3.12, 3.75, 1.7]
[5.53, 3.12, 2.1, 4.75]
[5.53, 3.12, 2.1, 1.7]
... <10 more entries omitted ...>
[1.05, 1.5, 2.1, 4.75]
[1.05, 1.5, 2.1, 1.7]
要打印产品而不是列表:
To print the products instead of the lists:
>>> import operator
>>> for choices in itertools.product([0, 1], repeat=len(off)):
... elts = [(on[i] if choice else off[i]) for i, choice in enumerate(choices)]
... print reduce(operator.mul, elts, 1)
...
307.32975
109.9917
172.10466
61.595352
...
如果您有可用的numpy并愿意使用numpy数组而不是Python列表,那么有一些不错的工具,例如
If you have numpy available and are willing to work with numpy arrays instead of Python lists, then there are some nice tools like numpy.choose available. For example:
>>> import numpy
>>> numpy.choose([0, 1, 0, 1], [off, on])
array([ 5.53, 1.5 , 3.75, 1.7 ])
>>> numpy.product(numpy.choose([0, 1, 0, 1], [off, on]))
52.880624999999995
与较早的解决方案结合可以提供:
Combining with the earlier solutions gives:
>>> for c in itertools.product([0, 1], repeat=4):
... print numpy.product(numpy.choose(c, [off, on]))
...
307.32975
109.9917
172.10466
61.595352
147.7546875
52.880625
...
这篇关于2个列表的Python组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!