0

for example this kind of for loop in JAVA or C++:

int N = 20;
for (i = 1; i < N; i = 3 * i + 1)

i will be 1, 4, 13

I can only use while to complete it

while i < N:
    i = 3 * i + 1

How can write it using another kind of style in python?

Sorry for my English.

4
  • 5
    The while loop is the best that I can think of. What are you actually doing with this? Commented May 24, 2013 at 15:39
  • If your increment were not 3 * i + 1, I would suggest looking into range but the step argument it accepts expects an integer. You could write your own version though. Commented May 24, 2013 at 15:49
  • Your for loop is valid in python Commented May 24, 2013 at 15:49
  • @karthikr but he's asking for the style in python, for which that loop is not the typical style regardless of whether it is valid. Commented May 24, 2013 at 15:52

3 Answers 3

3

Your while loop implementation is nothing wrong from Python's perspective. If you want a generalized loop structure to mimic the loop structure of C/C++/Java you can do something similar

>>> def loop(init, cond, incr):
    i = init
    while cond(i):
        yield i
        i=incr(i)


>>> list(loop(1, lambda e:e < 20, lambda e:3*e + 1))
[1, 4, 13]

Once you create the loop routine, you can use it to create any custom loop format as you desire

for i in loop(1, lambda e:e < 20, lambda e:3*e + 1):
    print i
Sign up to request clarification or add additional context in comments.

7 Comments

I think what the op wants is the 'Python style for', rather than C/Java for in Python.
@Ferguzz: The code is quite readable and maintainable if you know how to read it. The routine is a once of and can be reused for any form of series or loop structure.
@chenaren: The generator can be consumed by a Python for loop
This is the closest you can come to a range emulator which is what a typical person would need. I, however, disagree with the use of having cond be callable. It should be a simple integer. The incr argument should be callable though. Regardless, this isn't exactly what the OP is looking for as far as I can tell.
@sigmavirus24 If cond were an integer, what would that integer represent? Are you saying the condition should be hard coded to be i < cond? That would be significantly less general than a C-style for-loop (which can have arbitrary conditions). Most obviously it wouldn't allow you to write loops that count down.
|
0

This is a job for generators. Do this:

def series(i=1):
    while True:
        yield i
        i = 3 * i + 1

from itertools import takewhile
takewhile(lambda x: x < 20, series())

Comments

0

Here's another way to do it. It is more specialized than Abhijit's answer.

def timesThreePlusOne(init,limit):
    i = init
    while i < limit:
        yield i
        i = (3 * i) + 1
N = 20
for i in timesThreePlusOne(1,N):
    print i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.