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

J

Jean Dupont

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
 
R

Roy Smith

Jean Dupont said:
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

This sounds like a homework problem :)
As you can see the rows are always twice the size of the base
Why?

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.

What I get out of that is that you don't want to just print them, you
want to have some function which returns all the generated rows in
order. The way to do that is with the yield statement. Take a look at
https://wiki.python.org/moin/Generators for some discussion on how that
works. Actually, http://stackoverflow.com/questions/231767/
looks like an even better discussion.

Does that help you any?
 
P

Peter Otten

Jean said:
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():
.... 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
 
J

Jean Dupont

Op zondag 2 februari 2014 19:10:32 UTC+1 schreef Peter Otten:
Have a look at itertools.product():

... 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
 
A

Asaf Las

Op zondag 2 februari 2014 19:10:32 UTC+1 schreef Peter Otten:

I'm looking for an efficient method to produce rows of tables like this:
jean

you can also try to make below universal for all needed bases:

m = lambda m, n: 1 if m & n else 0
k = [[ m(x,8) , m(x, 4), m(x, 2), m(x, 1)] for x in range(10)]
print (k)
 
J

Jean Dupont

Op maandag 3 februari 2014 02:56:43 UTC+1 schreef Asaf Las:
Op zondag 2 februari 2014 19:10:32 UTC+1 schreef Peter Otten:

I'm looking for an efficient method to produce rows of tables like this:
jean
you can also try to make below universal for all needed bases:
m = lambda m, n: 1 if m & n else 0
k = [[ m(x,8) , m(x, 4), m(x, 2), m(x, 1)] for x in range(10)]
print (k)
Dear Asaf,
I'm not at ease with lamba-notation, could you show me how to modify your
example for the base 3 case? I guess then it will be much clearer to me

thanks in advance
jean
 
J

Jean Dupont

Op zondag 2 februari 2014 19:07:38 UTC+1 schreef Roy Smith:
This sounds like a homework problem :)


What I get out of that is that you don't want to just print them, you
want to have some function which returns all the generated rows in
order. The way to do that is with the yield statement. Take a look at
https://wiki.python.org/moin/Generators for some discussion on how that
works. Actually, http://stackoverflow.com/questions/231767/
looks like an even better discussion.

Does that help you any?

Thanks, I'll try to figure out what yield does

kind regards,
jean
 
A

Asaf Las

Op maandag 3 februari 2014 02:56:43 UTC+1 schreef Asaf Las:
you can also try to make below universal for all needed bases:
m = lambda m, n: 1 if m & n else 0
k = [[ m(x,8) , m(x, 4), m(x, 2), m(x, 1)] for x in range(10)]
print (k)

Dear Asaf,
I'm not at ease with lamba-notation, could you show me how to modify your
example for the base 3 case? I guess then it will be much clearer to me
thanks in advance

jean

I don't have to - use normal functions instead :)

/Asaf
 
J

Jean Dupont

Op maandag 3 februari 2014 16:34:18 UTC+1 schreef Asaf Las:
Op maandag 3 februari 2014 02:56:43 UTC+1 schreef Asaf Las:
10:32 UTC+1 schreef Peter Otten:
I'm looking for an efficient method to produce rows of tables like this:
jean
you can also try to make below universal for all needed bases:
m = lambda m, n: 1 if m & n else 0
k = [[ m(x,8) , m(x, 4), m(x, 2), m(x, 1)] for x in range(10)]
print (k)

Dear Asaf,
I'm not at ease with lamba-notation, could you show me how to modify your
example for the base 3 case? I guess then it will be much clearer to me
thanks in advance

jean

I don't have to - use normal functions instead :)
Of course you don't have to, but I'm curious and learn well by examples

:-(
 
A

Asaf Las

Op maandag 3 februari 2014 16:34:18 UTC+1 schreef Asaf Las:

Of course you don't have to, but I'm curious and learn well by examples
:-(

Hi Jean

Don't get me wrong i did not mean to be rude (was joking) - i
think if you will do it yourself that will be very good for
you - you can learn a lot from that as i did not very long time ago.
My apologies for inconvenience.

/Asaf
 
A

Asaf Las

Op maandag 3 februari 2014 16:34:18 UTC+1 schreef Asaf Las:

Of course you don't have to, but I'm curious and learn well by examples

:-(

And making this design generic is really a good example indeed.
 
T

Terry Reedy

Op maandag 3 februari 2014 02:56:43 UTC+1 schreef Asaf Las:

name = lambda xxx is poor style because it produces a function object
lacking a proper name. Reusing the function name as a local is also
confusing. The above is equivalent to

def m(k, n): return 1 if k & n else 0
k = [[ m(x,8) , m(x, 4), m(x, 2), m(x, 1)] for x in range(10)]
print (k)
Dear Asaf,
I'm not at ease with lamba-notation, could you show me how to modify your
example for the base 3 case? I guess then it will be much clearer to me

thanks in advance
jean
 
D

Denis McMahon

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
 
R

Rustom Mody

name = lambda xxx is poor style because it produces a function object
lacking a proper name. Reusing the function name as a local is also
confusing. The above is equivalent to
def m(k, n): return 1 if k & n else 0

Yeah... Well... Except that I'd put it the other way: the second --
shadowing names -- is MUCH worse than naked lambdas. Unless you're
entering an obfuscated python contest :D
 
J

Jean Dupont

Op maandag 3 februari 2014 20:50:04 UTC+1 schreef Asaf Las:
Hi Jean

Don't get me wrong i did not mean to be rude (was joking) - i
think if you will do it yourself that will be very good for
you - you can learn a lot from that as i did not very long time ago.
My apologies for inconvenience.
no hard feelings, anyway I am programming it myself using normal functions, when I have finished it I'll post it, then maybe you can do it with lamba-notation, it could be interesting to compare execution times for the two approaches

kind regards,
jean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top