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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With