Looping using iterators with fractional values

Discussion in 'Python' started by drife, Jan 1, 2005.

  1. drife

    drife Guest

    Hello,

    Making the transition from Perl to Python, and have a
    question about constructing a loop that uses an iterator
    of type float. How does one do this in Python?

    In Perl this construct quite easy:

    for (my $i=0.25; $i<=2.25; $i+=0.25) {
    printf "%9.2f\n", $i;
    }

    Thanks in advance for your help.


    Daran Rife
     
    drife, Jan 1, 2005
    #1
    1. Advertising

  2. drife wrote:

    > Hello,
    >
    > Making the transition from Perl to Python, and have a
    > question about constructing a loop that uses an iterator
    > of type float. How does one do this in Python?
    >
    >

    Use a generator:

    >>> def iterfloat(start, stop, inc):

    .... f = start
    .... while f <= stop:
    .... yield f
    .... f += inc
    ....
    >>> for x in iterfloat(0.25, 2.25, 0.25):

    .... print '%9.2f' % x
    ....
    0.25
    0.50
    0.75
    1.00
    1.25
    1.50
    1.75
    2.00
    2.25
    >>>
     
    Mark McEahern, Jan 1, 2005
    #2
    1. Advertising

  3. drife wrote:
    > Hello,
    >
    > Making the transition from Perl to Python, and have a
    > question about constructing a loop that uses an iterator
    > of type float. How does one do this in Python?
    >
    > In Perl this construct quite easy:
    >
    > for (my $i=0.25; $i<=2.25; $i+=0.25) {
    > printf "%9.2f\n", $i;
    > }


    <=Py2.3:

    for i in [x/4.0 for x in xrange(1, 10)]:
    print "%9.2f" % i

    Py2.4:

    for i in (x/4.0 for x in xrange(1, 20)):
    print "%9.2f" % i

    Reinhold
     
    Reinhold Birkenfeld, Jan 1, 2005
    #3
  4. Mark McEahern wrote:
    > drife wrote:
    >
    >> Hello,
    >>
    >> Making the transition from Perl to Python, and have a
    >> question about constructing a loop that uses an iterator
    >> of type float. How does one do this in Python?
    >>
    >>

    > Use a generator:
    >
    > >>> def iterfloat(start, stop, inc):

    > ... f = start
    > ... while f <= stop:
    > ... yield f
    > ... f += inc
    > ...
    > >>> for x in iterfloat(0.25, 2.25, 0.25):

    > ... print '%9.2f' % x
    > ...
    > 0.25
    > 0.50
    > 0.75
    > 1.00
    > 1.25
    > 1.50
    > 1.75
    > 2.00
    > 2.25
    > >>>

    >


    Or use the numarray module:

    py> import numarray as na
    py> for f in na.arange(0.25, 2.25, 0.25):
    .... print '%9.2f' % f
    ....
    0.25
    0.50
    0.75
    1.00
    1.25
    1.50
    1.75
    2.00

    Steve
     
    Steven Bethard, Jan 1, 2005
    #4
  5. drife

    Mike Meyer Guest

    "drife" <> writes:

    > Hello,
    >
    > Making the transition from Perl to Python, and have a
    > question about constructing a loop that uses an iterator
    > of type float. How does one do this in Python?
    >
    > In Perl this construct quite easy:
    >
    > for (my $i=0.25; $i<=2.25; $i+=0.25) {
    > printf "%9.2f\n", $i;
    > }



    Generally, you don't loop incrementing floating point values, as
    you're liable to be be surprised. This case will work as you expect
    because .25 can be represented exactly, but that's not always the
    case.

    Python loops are designed to iterate over things, not as syntactic
    sugar for while. So you can either do the while loop directly:

    i = 0.25
    while i <= 2.25:
    print "%9.2f" % i
    i += 0.25

    Or - and much safer when dealing with floating point numbers - iterate
    over integers and generate your float values:

    for j in range(1, 9):
    i = j * .25
    print "%9.2f" % i

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Jan 1, 2005
    #5
  6. Mike Meyer wrote:

    > Or - and much safer when dealing with floating point numbers - iterate
    > over integers and generate your float values:
    >
    > for j in range(1, 9):
    > i = j * .25
    > print "%9.2f" % i


    There's a glitch there, though - should be range(1, 10).

    Reinhold

    PS: I'm wondering whether my genexp approach or this one is preferable.
    Readability is equal, I would say, but how about speed?

    Brought up a few timeits:

    Python 2.3
    ----------

    for i in [x/4.0 for x in range(1, 10)]: 36,9 sec

    for j in range(1, 10): i = j * 0.25: 33,7 sec

    Python 2.4
    ----------

    for i in (x/4.0 for x in range(1, 10)): 32,5 sec

    for j in range(1, 10): i = j * 0.25: 28,4 sec


    So what does that tell us?
    (a) don't use genexps where there is a simpler approach
    (b) Py2.4 rocks!

    Reinhold
     
    Reinhold Birkenfeld, Jan 1, 2005
    #6
  7. drife

    Guest

    Mike Meyer wrote:

    >Or - and much safer when dealing with floating point numbers - iterate
    >over integers and generate your float values:


    >for j in range(1, 9):
    > i = j * .25
    > print "%9.2f" % i


    I agree with this suggestion. As an historical aside, Fortran had loops
    with floating point variables for decades, but in 1995, the first
    standard in a while to REMOVE features, this was one of the few things
    deleted. The Fortran standards committee is very conservative about
    creating backwards incompatibilities, but they must have thought loops
    with floating point variables are so error-prone -- and alternatives
    with integer counters are so easy to write -- that they put their foot
    down. I know the OP is asking about Python, but the principle is the
    same.
     
    , Jan 1, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maurer
    Replies:
    3
    Views:
    5,087
    Peter
    Apr 19, 2006
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    519
    Kai-Uwe Bux
    May 8, 2005
  3. srinivasan srinivas
    Replies:
    3
    Views:
    309
    John Machin
    Nov 19, 2008
  4. Chris Rebert
    Replies:
    0
    Views:
    1,452
    Chris Rebert
    Nov 20, 2008
  5. , India
    Replies:
    10
    Views:
    1,104
    James Kanze
    Aug 8, 2009
Loading...

Share This Page