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

Jean Dupont jeandupont314 at gmail.com
Sun Feb 2 21:51:15 CET 2014


Op zondag 2 februari 2014 19:10:32 UTC+1 schreef Peter Otten:
> 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

Thanks for the suggestion I'm going to look into it further

kind regards,
jean



More information about the Python-list mailing list