[Numpy-discussion] how do I list all combinations

Stuart Brorson sdb@cloud9....
Wed Dec 26 15:03:59 CST 2007


Hi --

>> I have an arbitrary number of lists. I want to form all possible
>> combinations from all lists. So if
>> r1=["dog","cat"]
>> r2=[1,2]
>>
>> I want to return [["dog",1],["dog",2],["cat",1],["cat",2]]

Try this:

In [25]: [(x, y) for x in r1 for y in r2]
Out[25]: [('dog', 1), ('dog', 2), ('cat', 1), ('cat', 2)]


Cheers,

Stuart Brorson
Interactive Supercomputing, inc.
135 Beaver Street | Waltham | MA | 02452 | USA
http://www.interactivesupercomputing.com/


More information about the Numpy-discussion mailing list