[Tutor] Geometric sequence
bob gailer
bgailer at gmail.com
Thu Oct 31 04:00:18 CET 2013
On 10/30/2013 1:08 PM, 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.
import itertools
series = [2**x for x in itertools.takewhile(lambda x: 2**x<60, range(7))]
if the limit is really large you could calculate it once:
import itertools
import math
limit = int(math.log(60)) + 1
series = [2**x for x in itertools.takewhile(lambda x:x<limit, range(7))]
which in turn leads to:
import math
series = [2**x for x in range(int(math.log(60)) + 2)]
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list