0

If I'm trying to move through a very small range, and want to refer to a variable created in an earlier step of the iteration, how can I reference it?

If I use a for loop with a range of say 2,6, and on the first iteration I create two variables a[i] & b[i], is it possible to reference a3 on the 4th or 5th iteration? Or something like doSomething(a[i-1],a[i])

Apologies for the terrible psuedo-code.

1
  • 1
    It would be much better if you gave a concrete example, with meaningful names, and actual code. Not only would it help other people with similar problems to find your question and benefit from any answers, it would make it a lot easier for you to understand the answers.
    – abarnert
    Commented Sep 12, 2014 at 5:04

1 Answer 1

1

You can't "manipulate the iterator". But you can do two simple things.

First, since you're apparently iterating over sequences (like range), you can use indices. For example:

for i, value in enumerate(my_sequence):
    if 3 <= i <= 4:
        old_value = my_sequence[i-2]
        do_something(old_value, value)

Second, you can just remember the old values:

lastlast = last = None
for value in my_sequence:
    if lastlast is not None:
        do_something(lastlast, value)
    lastlast, last = last, value

If you want to make the second one more readable, you can pretty easily write a function that takes any iterable and iterates over overlapping groups of 3 values, so you can just do this:

for lastlast, last, value in triplets(my_sequence):
    do_something(lastlast, value)

So, how do you write that triplets function?

def triplets(iterable):
    a, b, c = itertools.tee(iterable, 3)
    next(b, None)
    next(c, None)
    next(c, None)
    return itertools.izip(a, b, c)

Or, if you only care about sequences, not iterables in general, and are willing to waste some space to maybe save a little time and definitely save a bit of code:

def triplets(sequence):
    return zip(sequence, sequence[1:], sequence[2:])
4
  • @PM2Ring: Thanks, but… do you not have enough rep to edit it yourself?
    – abarnert
    Commented Sep 12, 2014 at 7:30
  • I guess I do, but I didn't want to tread on your toes, so to speak. But in future I shall! :) I'm only new & still getting used to how this place works...
    – PM 2Ring
    Commented Sep 12, 2014 at 7:36
  • @PM2Ring: If you have enough rep to actually edit, you should definitely fix typos that are significant like that; anyone who gets annoyed is missing the point of SO. But if you only have enough rep to suggest an edit, be more careful. Suggested edits have to go through the review queue, and people get annoyed at wading through lots of small edits and may reject them even if they are significant. (Anyway, I didn't realize you were knew here, you seemed more experienced. I almost said "sorry", but I guess that's not something to apologize for.:)
    – abarnert
    Commented Sep 12, 2014 at 7:49
  • I'm new here, but I have been programming for a few decades... :)
    – PM 2Ring
    Commented Sep 12, 2014 at 8:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.