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

Denis McMahon denismfmcmahon at gmail.com
Mon Feb 3 19:11:26 EST 2014


On Sun, 02 Feb 2014 09:44:05 -0800, 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
> 
> kind regards and thanks in advance jean

s=3
p=s*2

def b(n,x):
	s=[]
	while n:
		s.append(str(n%x))
		n=n/x
	if s==[]:
		return "0"
	return ''.join(s[::-1])

for i in range(s**p):
	r=("{:0"+str(p)+"d}").format(int(b(i,s)))
	if len(r)==p:
		print [int(r[a])for a in range(len(r))]

change s to 2 or 4 etc

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list