Little novice program written in Python
Robert Bossy
Robert.Bossy at jouy.inra.fr
Fri Apr 25 04:24:16 EDT 2008
John Machin wrote:
> On Apr 25, 5:44 pm, Robert Bossy <Robert.Bo... at jouy.inra.fr> wrote:
>
>> Peter Otten wrote:
>>
>>> Rogério Brito wrote:
>>>
>>>> i = 2
>>>> while i <= n:
>>>> if a[i] != 0:
>>>> print a[i]
>>>> i += 1
>>>>
>>> You can spell this as a for-loop:
>>>
>>> for p in a:
>>> if p:
>>> print p
>>>
>>> It isn't exactly equivalent, but gives the same output as we know that a[0]
>>> and a[1] are also 0.
>>>
>> If the OP insists in not examining a[0] and a[1], this will do exactly
>> the same as the while version:
>>
>> for p in a[2:]:
>> if p:
>> print p
>>
>>
>
> ... at the cost of almost doubling the amount of memory required.
Indeed. Would it be a sensible proposal that sequence slices should
return an iterator instead of a list?
RB
More information about the Python-list
mailing list