Python中的成对叉积 [英] Pairwise crossproduct in Python
本文介绍了Python中的成对叉积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从Python中任意长列表中获取叉积对的列表?
How can I get the list of cross product pairs from a list of arbitrarily long lists in Python?
a = [1, 2, 3]
b = [4, 5, 6]
crossproduct(a,b)
应该产生[[1, 4], [1, 5], [1, 6], ...]
.
推荐答案
You're looking for itertools.product if you're on (at least) Python 2.6.
>>> import itertools
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> itertools.product(a,b)
<itertools.product object at 0x10049b870>
>>> list(itertools.product(a,b))
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
这篇关于Python中的成对叉积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文