[Tutor] Geometric sequence

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Oct 31 01:35:14 CET 2013


On Oct 31, 2013 12:10 AM, "Mark Lawrence" <breamoreboy at yahoo.co.uk> wrote:
>
> On 30/10/2013 17:08, Peter O'Doherty wrote:
>>
>> Hi List,
>>
>> I know a geometric sequence can be produced by:
>>
>> series = [2**x for x in range(7)]
>>
>> But I would like to curtail the sequence before the last element excedes
>> a certain value. Is there a better way of doing it that the following:
>>
>> for x in range(20):
>>      series_element = 2**x
>>      print series_element
>>      if series_element > 60:
>>          break
>> print series
>>
>> Many thanks,
>> Peter
>
>
> I don't actually know and I'll confess to being too lazy to look, but is
there anything in the itertools module that would do this?

There's takewhile. An ordinary while loop is probably better though.

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131031/49297073/attachment.html>


More information about the Tutor mailing list