[Tutor] Fwd: range() fractional increment

Richard Lovely roadierich at googlemail.com
Wed Apr 1 19:14:16 CEST 2009


forgot to reply-all...

---------- Forwarded message ----------
From: Richard Lovely <roadierich at googlemail.com>
Date: 1 Apr 2009 18:13
Subject: Re: [Tutor] range() fractional increment
To: Alan Gauld <alan.gauld at btinternet.com>


There's always the itertools solution:

import itertools
def rangeInThirds(start,end):
   for whole, frac in itertools.product(range(start,end), (0, 0.3, 0.6)):
       yield whole + frac

The simplist way is to use a list comprehension:
[whole + frac for whole in xrange(start, end) for frac in [0, 0.3, 0.6]]

Then you just need to turn the iterable (generator in the first case,
list in the second into your "array".

On 31/03/2009, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "james carnell" <jimcarnell at yahoo.com> wrote
>
> > example:
> > x0000 row = 25 : col = 10
> > x0000 row = 26 : col = 10.3
> > x0000 row = 27 : col = 10.6
> > 0x000 row = 28 : col = 11
> >
>
> > for col in range(10,12, 0.3): #<- Crash Bang doesn't work 0.3 = zero =
> infinite loop?
> >
>
> If you know the limits (rather than their being variables) you can do
>
> for n in range(100,120,3)
>    n = n/10
>
> But if you have variable range limits then a generator
> or while loop are your best bets I think.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/l2p/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


--
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com


-- 
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com


More information about the Tutor mailing list