Possible Duplicate:
Get the cartesian product of a series of lists in Python
Suppose I have an array of length n, representing n variables, and a function f of n variables. I want to sum over f applied to all values of the n variables in some finite set (i.e. {0,1}). Conceptually, it would be something like
for x[1] in {0,1}:
for x[2] in {0,1}:
...
sum += f(x[1], ..., x[n])
but obviously you can't write this.
Is there a nice way to do it, say in Python? (For the particular case of values in {0,1}, I could just loop over binary representations of integers from 0 to 2^n-1, but I want a more general solution).