What is the "functional" way of doing this?
Steven D'Aprano
steve at REMOVE.THIS.cybersource.com.au
Mon Jul 30 19:34:31 EDT 2007
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.
More information about the Python-list
mailing list