[newbie] making rows of table with discrete values for different number systems

Peter Otten __peter__ at web.de
Sun Feb 2 13:10:32 EST 2014


Jean Dupont wrote:

> I'm looking for an efficient method to produce rows of tables like this:
> 
> for base 2
> 0 0 0 0
> 0 0 0 1
> 0 0 1 0
> 0 0 1 1
> 0 1 0 0
> .
> .
> .
> 1 1 1 1
> 
> for base 3
> 0 0 0 0 0 0
> 0 0 0 0 0 1
> 0 0 0 0 0 2
> 0 0 0 0 1 0
> 0 0 0 0 1 1
> 0 0 0 0 1 2
> .
> .
> 2 2 2 2 2 2
> 
> As you can see the rows are always twice the size of the base
> I _don't_ need to have all rows available together in one array which
> would become too large for higher value number bases. It's sufficient to
> produce one row after the other, as I will do further data manipulation on
> such a row immediately.
> 
> If someone here could suggest best practices to perform this kind of
> operations,I'd really appreciate it very much

Have a look at itertools.product(): 

>>> import itertools
>>> for row in itertools.product(range(2), repeat=4):
...     print(*row)
... 
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





More information about the Python-list mailing list