Binary numbers

Kenneth Loafman ken at lt.com
Tue Feb 6 14:01:31 EST 2001


This is my second post on this subject.  Mail to this user bounces. 
Could be a troll, but since I'd already answered, here's what I said
(important to conserve electrons, don't you know)...

sampe99 at my-deja.com wrote:
> 
> Does anyone know an easy way to get a list of binary numbers within a
> certain range in python? E.g for
> n=1 [0],[1]
> n=2 [0,0],[0,1],[1,0],[1,1]
> n=3 [0,0,0],[0,0,1] a.s.o
> 
> I need this for n<=18...

With n=18, you're asking for
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1],
...
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] 

n=18 would be a huge list of lists, (2^18) * 18 in size.

Assuming 4-byte integers, that would be (262144 * 18 * 4) = 18,874,368
which is 18MB in data alone.  Throw in the python list overhead and it
would probably occupy 25-30MB.  Of course, printing it out would be
impractical (useless?).

Any practical use for such a list?

...Ken




More information about the Python-list mailing list