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

Re: Getting original working directory
Re: 400 Bad Request calling urllib2.urlopen()
Re: I want py2exe not to create library.zip
Re: Exact Match and Identity conversion
Re: Python 3K or Python 2.9?
wczasy turcja 2010
audiobooki
meble kuchenne
bajki dla dzieci
Zdrowie i uroda