On Fri, Sep 14, 2012 at 9:47 PM, Alexander Blinne <n...@blinne.net> wrote:
> On 14.09.2012 00:38, Chris Angelico wrote:
>> On Fri, Sep 14, 2012 at 8:33 AM, Alexander Blinne <n...@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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to