0

This may be a simple task, but I am unsure of how to achieve this in Python.

I have a for loop executing on an index in Python. I have a unique value that is defined within each iteration that is cycled through the for loop.

I want to get the value of the NEXT or PREVIOUS for loop unique value. For example, I have:

counter = 0

for rect in rects:
    randomnumber = random.randint(1,101)
    if counter < len(rects)-1:
        if rects[counter] - rects[counter+1]
            pastrand = {get random value from PREVIOUS loop iteration}
            randsubtract = randomnumber - pastrand

So how do I get the random number from the previous (or next) iteration to use in the CURRENT iteration in Python? For example:

  randomnumber in rects[0]
  randomnumber in rects[1]

How do I call specific values from iterations in for loops?

3 Answers 3

2

its late in the day, but this might do you..

counter = 0
random =[]
for rect in rects:
    randomnumber = random.randint(1,101)
    random.append(randomnumber)
    if counter < len(rects)-1:
        if rects[counter] - rects[counter+1]:
        pastrand = random[-1]
        randsubtract = randomnumber - pastrand
Sign up to request clarification or add additional context in comments.

1 Comment

Ok I’ve definitely been staring at this way too long - it didn’t even occur that I can just index the current random number into a new index with [counter] as the iteration! Thank you. This works!
1

Option 1
Use enumerate. If you want the current and next, you'll need to iterate till len(rect) - 1. If you want the previous and current, you'll need to start iterating from 1.

for i, r in enumerate(rects[:-1]):
    cur = r
    next = rects[i + 1]

Or,

for i, r in enumerate(rects[1:]):
    prev = rects[-1]
    cur = r

Option 2
You can use zip to the same effect:

for cur, next in zip(rect, rect[1:]):
    ...

2 Comments

he would have to error handle if it goes out of bounds right?
@MikeTung The whole If you want the current and next, you'll need to iterate till len(rect) - 1. If you want the previous and current, you'll need to start iterating from 1. is to handle out of bounds.
1

As written, you can save the value from the previous loop iteration before assigning a new one.

for ...:
    pastrand = randomnumber
    randomnumber = ...

Of course you will have to assign something to randomnumber before the loop starts so that the assignment works the very first time through.

An alternative would be to loop over pairs of random numbers rather than computing one random number per loop iteration. For this you can use the pairwise() tool whose implementation is given in the itertools documentation or e.g. in the more-itertools package. Looping over pairs of random numbers could be done like this:

for rand1, rand2 in pairwise(repeatfunc(random.randint, None, 1, 101)):
    ...

where I have used another itertool, repeatfunc(), to repeatedly call randint(). (You can do this without using repeatfunc() too.) At each iteration of this loop except the first, rand1 will be equal to rand2 from the previous iteration.

Now, you're going to want to pair each random number with a rectangle (assuming that's what is in rects), right? That you can do using zip(). Specifically, zip(random_numbers, rects) is an iterator over tuples of a random number and a rectangle. You could use it like so:

for randomnumber, rect in zip(random_numbers, rects):
    ...

but you're going to want to iterate over pairs, so you combine pairwise with that:

for r1, r2 in pairwise(zip(random_numbers, rects)):
    rand1, rect1 = r1
    rand2, rect2 = r2
    ...

Here random_numbers could be that thing I did earlier with repeatfunc(). This will associate one random number with each rectangle, and give you access to each set of two consecutive number/rectangle pairs.

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.