Python presentations

Chris Angelico rosuav at gmail.com
Fri Sep 14 08:19:22 EDT 2012


On Fri, Sep 14, 2012 at 9:47 PM, Alexander Blinne <news at blinne.net> wrote:
> On 14.09.2012 00:38, Chris Angelico wrote:
>> On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <news at blinne.net> wrote:
>>> def powerlist(x,n):
>>>     if n==1:
>>>         return [1]
>>>     p = powerlist(x,n-1)
>>>     return p + [p[-1]*x]
>>
>> Eh, much simpler.
>>
>> def powerlist(x,n):
>>   return [x*i for i in xrange(n-1)]
>
> I suppose you meant:
>
> def powerlist(x,n):
>   return [x**i for i in xrange(n-1)]
>
> But this is less efficient, because it needs more multiplications (see
> Horner's method)

Err, yes, I did mean ** there. The extra multiplications may be
slower, but which is worse? Lots of list additions, or lots of integer
powers? In the absence of clear and compelling evidence, I'd be
inclined to go with the list comp - and what's more, to skip this
function altogether and use the list comp directly where it's needed.

ChrisA



More information about the Python-list mailing list