Binary numbers

Mark Pilgrim f8dy at my-deja.com
Tue Feb 6 17:54:07 CET 2001


In article <95p7ks$9e6$1 at nnrp1.deja.com>,
  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

>>> def b(l): return l>1 and [x+y for x in ['0','1'] for y in b(l-1)]
or ['0','1']
>>> b(1)
['0', '1']
>>> b(2)
['00', '01', '10', '11']
>>> b(3)
['000', '001', '010', '011', '100', '101', '110', '111']

Why do I get the feeling I'm doing your CS homework? ;)

-M
--
You're smart; why haven't you learned Python yet?
http://diveintopython.org/


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list