The following code generates all possible combinations using 0
and 1
, where four digits have to be used.
import itertools
for i in itertools.product([0, 1], repeat=4):
print i
Output:
(0, 0, 0, 0)(0, 0, 0, 1)(0, 0, 1, 0)(0, 0, 1, 1)(0, 1, 0, 0)(0, 1, 0, 1)(0, 1, 1, 0)(0, 1, 1, 1)(1, 0, 0, 0)(1, 0, 0, 1)(1, 0, 1, 0)(1, 0, 1, 1)(1,1, 0, 0)(1, 1, 0, 1)(1, 1, 1, 0)(1, 1, 1, 1)
I would like to be able to set an offset for the for loop. Example:
import itertools
offSet = 10
for i in itertools.product([0, 1], repeat=4):
# Some code that applies the offset
print i
Which would then output:
(1, 0, 1, 0)(1, 0, 1, 1)(1,1, 0, 0)(1, 1, 0, 1)(1, 1, 1, 0)(1, 1, 1, 1)
How can I apply a such offset to this for loop?
Note: The code being used is simplified. As I am actually using a very large value for repeat
, performance matters. I cannot afford the possible combinations before the offset to be calculated.
How about this:
In [29]: offSet = 10
In [30]: repeat = 4
In [31]: for i in xrange(offSet, 2**repeat):
print tuple(int(x) for x in bin(i)[2:])
....:
(1, 0, 1, 0)
(1, 0, 1, 1)
(1, 1, 0, 0)
(1, 1, 0, 1)
(1, 1, 1, 0)
(1, 1, 1, 1)