Home

On Mon, 30 Jul 2007 22:48:10 +0000, beginner wrote:

> Hi,
>
> If I have a number n and want to generate a list based on like the
> following:
>
> def f(n):
> l=[]
> while n>0:
> l.append(n%26)
> n /=26
> return l
>
> I am wondering what is the 'functional' way to do the same.


Seems like a perfectly good function to me :)


I don't know about "functional", but that's crying out to be written as a
generator:

def f(n):
while n > 0:
n, x = divmod(n, 26)
yield x



And in use:
>>> for x in f(1000):
.... print x
....
12
12
1
>>> list(f(1000))
[12, 12, 1]


--
Steven.

previous
next

The meaning of a = b in object oriented languages
Re: correction
Re: Iterator to last element in list?
Re: wxPython before MainLoop
Re: map (associative array) loses values?
okna pcv
teksty
florida
car hire cyprus
sonyericsson
noclegi poznań
księga wieczysta
analiza finansowa
http://katalog.info-tour.com.pl
Dywany
bluzki