473,578 Members | 3,102 Online

# Looping using iterators with fractional values

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;
}

Daran Rife
da**********@PA Myahoo.com

Jul 18 '05 #1
6 1731
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

Jul 18 '05 #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?

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
Jul 18 '05 #3
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
Jul 18 '05 #4
"drife" <da*******@yaho o.com> 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 <mw*@mired.or g> http://www.mired.org/home/mwm/
Jul 18 '05 #5
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.

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
Jul 18 '05 #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

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 incompatibiliti es, 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.

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.