如何从一组列表中获取笛卡尔积(每种可能的值组合)?
输入:
somelists = [
[1, 2, 3],
['a', 'b'],
[4, 5]
]
期望的输出:
[(1, 'a', 4), (1, 'a', 5), (1, 'b', 4), (1, 'b', 5), (2, 'a', 4), (2, 'a', 5) ...]
答案 0 :(得分:314)
在Python 2.6 +
中import itertools
for element in itertools.product(*somelists):
print(element)
答案 1 :(得分:70)
import itertools
>>> for i in itertools.product([1,2,3],['a','b'],[4,5]):
... print i
...
(1, 'a', 4)
(1, 'a', 5)
(1, 'b', 4)
(1, 'b', 5)
(2, 'a', 4)
(2, 'a', 5)
(2, 'b', 4)
(2, 'b', 5)
(3, 'a', 4)
(3, 'a', 5)
(3, 'b', 4)
(3, 'b', 5)
>>>
答案 2 :(得分:33)
对于Python 2.5及更早版本:
>>> [(a, b, c) for a in [1,2,3] for b in ['a','b'] for c in [4,5]]
[(1, 'a', 4), (1, 'a', 5), (1, 'b', 4), (1, 'b', 5), (2, 'a', 4),
(2, 'a', 5), (2, 'b', 4), (2, 'b', 5), (3, 'a', 4), (3, 'a', 5),
(3, 'b', 4), (3, 'b', 5)]
这是product()
的递归版本(仅举例说明):
def product(*args):
if not args:
return iter(((),)) # yield tuple()
return (items + (item,)
for items in product(*args[:-1]) for item in args[-1])
示例:
>>> list(product([1,2,3], ['a','b'], [4,5]))
[(1, 'a', 4), (1, 'a', 5), (1, 'b', 4), (1, 'b', 5), (2, 'a', 4),
(2, 'a', 5), (2, 'b', 4), (2, 'b', 5), (3, 'a', 4), (3, 'a', 5),
(3, 'b', 4), (3, 'b', 5)]
>>> list(product([1,2,3]))
[(1,), (2,), (3,)]
>>> list(product([]))
[]
>>> list(product())
[()]
答案 3 :(得分:17)
import itertools
result = list(itertools.product(*somelists))
答案 4 :(得分:11)
在Python 2.6及更高版本中,您可以使用'itertools.product`。在旧版本的Python中,您可以使用以下(几乎 - 请参阅文档)等效code from the documentation,至少作为起点:
def product(*args, **kwds):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
两者的结果都是迭代器,所以如果你真的需要一个列表进行进一步处理,请使用list(result)
。
答案 5 :(得分:10)
我会使用列表理解:
somelists = [
[1, 2, 3],
['a', 'b'],
[4, 5]
]
cart_prod = [(a,b,c) for a in somelists[0] for b in somelists[1] for c in somelists[2]]
答案 6 :(得分:9)
这是一个递归生成器,它不存储任何临时列表
def product(ar_list):
if not ar_list:
yield ()
else:
for a in ar_list[0]:
for prod in product(ar_list[1:]):
yield (a,)+prod
print list(product([[1,2],[3,4],[5,6]]))
输出:
[(1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6), (2, 3, 5), (2, 3, 6), (2, 4, 5), (2, 4, 6)]
答案 7 :(得分:6)
虽然已有很多答案,但我想分享一下我的想法:
def cartesian_iterative(pools):
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
return result
def cartesian_recursive(pools):
if len(pools) > 2:
pools[0] = product(pools[0], pools[1])
del pools[1]
return cartesian_recursive(pools)
else:
pools[0] = product(pools[0], pools[1])
del pools[1]
return pools
def product(x, y):
return [xx + [yy] if isinstance(xx, list) else [xx] + [yy] for xx in x for yy in y]
def cartesian_reduct(pools):
return reduce(lambda x,y: product(x,y) , pools)
答案 8 :(得分:2)
只是为已经说过的内容添加一点:如果你使用sympy,你可以使用符号而不是字符串,这使得它们在数学上有用。
import itertools
import sympy
x, y = sympy.symbols('x y')
somelist = [[x,y], [1,2,3], [4,5]]
somelist2 = [[1,2], [1,2,3], [4,5]]
for element in itertools.product(*somelist):
print element
关于sympy。
答案 9 :(得分:2)
对可变风格的上述递归生成器解决方案的一个小修改:
def product_args(*args):
if args:
for a in args[0]:
for prod in product_args(*args[1:]) if args[1:] else ((),):
yield (a,) + prod
当然是一个包装器,它使它与该解决方案完全相同:
def product2(ar_list):
"""
>>> list(product(()))
[()]
>>> list(product2(()))
[]
"""
return product_args(*ar_list)
一次权衡:它检查递归是否应该在每个外部循环中断开,并且一次增益:空调时没有收益,例如product(())
,我认为在语义上更正确(参见doctest)。
关于列表理解:数学定义适用于任意数量的参数,而列表理解只能处理已知数量的参数。
答案 10 :(得分:2)
递归方法:
def rec_cart(start, array, partial, results):
if len(partial) == len(array):
results.append(partial)
return
for element in array[start]:
rec_cart(start+1, array, partial+[element], results)
rec_res = []
some_lists = [[1, 2, 3], ['a', 'b'], [4, 5]]
rec_cart(0, some_lists, [], rec_res)
print(rec_res)
迭代方法:
def itr_cart(array):
results = [[]]
for i in range(len(array)):
temp = []
for res in results:
for element in array[i]:
temp.append(res+[element])
results = temp
return results
some_lists = [[1, 2, 3], ['a', 'b'], [4, 5]]
itr_res = itr_cart(some_lists)
print(itr_res)
答案 11 :(得分:1)
我相信这可行:
def cartesian_product(L):
if L:
return {(a,) + b for a in L[0]
for b in cartesian_product(L[1:])}
else:
return {()}
答案 12 :(得分:0)
Stonehenge方法:
def giveAllLists(a, t):
if (t + 1 == len(a)):
x = []
for i in a[t]:
p = [i]
x.append(p)
return x
x = []
out = giveAllLists(a, t + 1)
for i in a[t]:
for j in range(len(out)):
p = [i]
for oz in out[j]:
p.append(oz)
x.append(p)
return x
xx= [[1,2,3],[22,34,'se'],['k']]
print(giveAllLists(xx, 0))
输出:
[[1, 22, 'k'], [1, 34, 'k'], [1, 'se', 'k'], [2, 22, 'k'], [2, 34, 'k'], [2, 'se', 'k'], [3, 22, 'k'], [3, 34, 'k'], [3, 'se', 'k']]
答案 13 :(得分:0)
def my_product(pools: List[List[Any]], rules: Dict[Any, List[Any]], forbidden: List[Any]) -> Iterator[Tuple[Any]]:
"""
Compute the cartesian product except it rejects some combinations based on provided rules
:param pools: the values to calculate the Cartesian product on
:param rules: a dict specifying which values each value is incompatible with
:param forbidden: values that are never authorized in the combinations
:return: the cartesian product
"""
if not pools:
return
included = set()
# if an element has an entry of 0, it's acceptable, if greater than 0, it's rejected, cannot be negative
incompatibles = defaultdict(int)
for value in forbidden:
incompatibles[value] += 1
selections = [-1] * len(pools)
pool_idx = 0
def current_value():
return pools[pool_idx][selections[pool_idx]]
while True:
# Discard incompatibilities from value from previous iteration on same pool
if selections[pool_idx] >= 0:
for value in rules[current_value()]:
incompatibles[value] -= 1
included.discard(current_value())
# Try to get to next value of same pool
if selections[pool_idx] != len(pools[pool_idx]) - 1:
selections[pool_idx] += 1
# Get to previous pool if current is exhausted
elif pool_idx != 0:
selections[pool_idx] = - 1
pool_idx -= 1
continue
# Done if first pool is exhausted
else:
break
# Add incompatibilities of newly added value
for value in rules[current_value()]:
incompatibles[value] += 1
included.add(current_value())
# Skip value if incompatible
if incompatibles[current_value()] or \
any(intersection in included for intersection in rules[current_value()]):
continue
# Submit combination if we're at last pool
if pools[pool_idx] == pools[-1]:
yield tuple(pool[selection] for pool, selection in zip(pools, selections))
# Else get to next pool
else:
pool_idx += 1
我在 a case 处必须获取一个非常大的笛卡尔积的第一个结果。尽管我只想要一件物品,但这需要很长时间。问题在于,由于结果的顺序,它必须遍历许多不需要的结果才能找到正确的结果。因此,如果我有 10 个包含 50 个元素的列表,并且前两个列表的第一个元素不兼容,则它必须遍历最后 8 个列表的笛卡尔积,尽管它们都会被拒绝。
此实现允许在结果包含每个列表中的一项之前对其进行测试。因此,当我检查某个元素是否与之前列表中已包含的元素不兼容时,我会立即转到当前列表的下一个元素,而不是遍历以下列表的所有产品。
答案 14 :(得分:0)
以下代码是 Using numpy to build an array of all combinations of two arrays 的 95% 副本,所有学分都在那里!据说这要快得多,因为它只在 numpy 中。
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9",
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9"
如果您不想从所有条目的第一个条目中获取 dtype,则需要将 dtype 定义为参数。如果您有字母和数字作为项目,则取 dtype = 'object'。测试:
private val actor = actor<CelebrationAction>(UI, Channel.CONFLATED) {
for (action in this) when (action) {
}
}
}
出:
setTimeout(()=>
document.querySelector("meta[name=viewport]").setAttribute("content", "height=" + screen.height*0.9 + "px, width=device-width, initial-scale=1.0")
, 300);
答案 15 :(得分:-1)
您可以在标准库中使用itertools.product
来获得笛卡尔积。 itertools
中其他很酷的相关实用程序包括permutations
,combinations
和combinations_with_replacement
。这是以下代码段的a link到python codepen:
from itertools import product
somelists = [
[1, 2, 3],
['a', 'b'],
[4, 5]
]
result = list(product(*somelists))
print(result)