Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I build a list of all possible tuples from this table?

Suppose I have a set of column definitions:

Col1: value11 value12 value13
Col2: value21 value22 
Col3: value31 value32 value33
...

Given some subset of columns -- 2 or more -- I want to find all possible values for those columns. Suppose I choose columns 1 and 2, above:

(value11 value21)
(value11 value22)
(value12 value21)
(value12 value22)
(value13 value21)
(value13 value22)

If I'd chosen 2:3:

(value21 value31)
(value21 value32)
(value21 value33)
(value22 value31)
(value22 value32)
(value22 value33)

If I'd chosen all three:

(value11 value21 value31)
(value11 value21 value32)
...

I'm implementing this in python, and I'd like a fast algorithm to do this. My input is a list of tuples: (columnName, columnValueList)

Any suggestions?

like image 284
Chris R Avatar asked Nov 30 '22 20:11

Chris R


1 Answers

The best way to get this is going to be using itertools.product(). For example:

import itertools

group1 = ['a', 'b']
group2 = ['c', 'd']

print list(itertools.product(group1, group2))

#==> [('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')]

This function accepts multiple arguments (i.e. multiple columns).

For more help on iterools.product() see this.

like image 132
Jeremy Cantrell Avatar answered Dec 06 '22 09:12

Jeremy Cantrell