[Numpy-discussion] how do I list all combinations
Alan G Isaac
Sun Dec 30 12:25:07 CST 2007
On Wed, 26 Dec 2007, Mathew Yeates apparently wrote:
> I want to return [["dog",1],["dog",2],["cat",1],["cat",2]]
This is a Cartesian product.
Sage has ``cartesian_product_iterator`` for this.
Here is a Cookbook implementation.
The generator may be adequate to your needs.
Here is a recursive implementation that does not use
last_set = sets[-1]
drop_last = sets[:-1]
result = set( x+(y,)
for x in set_product(*drop_last)
for y in last_set )
result = set( (y,) for y in last_set )
Sorry for a late reply. I'm catching up on email...
More information about the Numpy-discussion