0

I want to compare certain numbers I have in a list with values from another list by creating a range. Like this:

r = np.arange(0, 20, 2)

Now, r is an array that looks like this:

array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])

Now, I would like to use a for loop starting with the first two elements of r, and create a range, such that for the first iteration the 1st and 2nd elements are considered, then for the second iteration, the 2nd and 3rd elements are considered.

So it looks like this for each iteration:

range(0,2)
range(2,4)
range(4,6)
range(6,8)

and so on.

Is there a function to loop through in this way?

I don't want to iterate over non-overlapping chunks, i.e.

range(0,2)
range(4,6) # This is not what I want
range(6,8)

and so on.

nekomatic
  • 5,988
  • 1
  • 20
  • 27
  • @mkrieger1 Not exactly, that question is about splitting a list into non-overlapping chunks whereas here the asker wants [0, 2], [2, 4], [4, 6] etc. – nekomatic Mar 15 '21 at 15:15
  • Do you want the ranges or an array? This seems like an XY problem. What do you plan on doing with the result? – Mad Physicist Mar 15 '21 at 15:16
  • @MadPhysicist I have a second list of numbers, and I want to check for every number in the second list, if it falls within every range. And then print how many numbers fall in each range. – Varad Kulkarni Mar 15 '21 at 15:20
  • Please don't close the question. I do not want to iterate over the array in chunks. Like @nekomatic has explained. – Varad Kulkarni Mar 15 '21 at 15:25
  • You have an array [0, 2, 4, 6, ...] and you want to split it into [0, 2], [4, 6], ... that's exactly what the linked question is about. (on top of that you can put the resulting pairs as arguments into `range()`) – mkrieger1 Mar 15 '21 at 15:30
  • No, I want to split it into `range(0,2), range(2,4), range(4,6)` etc. – Varad Kulkarni Mar 15 '21 at 15:32
  • What about the last paragraph? You seem to have two conflicting requirements. – mkrieger1 Mar 15 '21 at 15:32
  • I wrote that because the same question you suggested was suggested by someone else. So I edited my question to clarify that I am not looking to iterate over 'chunks'. – Varad Kulkarni Mar 15 '21 at 15:33
  • Please ask a clear question about your end goal with exact inputs and outputs. Ranges are not a good way to go about that. – Mad Physicist Mar 15 '21 at 15:34
  • 1
    Anyway, the other requirement still has many answers already: for example, https://stackoverflow.com/questions/5434891/iterate-a-list-as-pair-current-next-in-python – mkrieger1 Mar 15 '21 at 15:35
  • @mkrieger1 agreed, this should be re-opened then correctly closed as a duplicate of that question. – nekomatic Mar 16 '21 at 12:27

1 Answers1

1

Update: Since Python 3.10, this functionality is built-in. Use itertools.pairwise().

itertools has a nice recipe at the bottom called "pairwise":

from itertools import tee

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

Example usage:

for x, y in pairwise([1,2,3,4]):
     print(x, y)

1 2
2 3
3 4
Bharel
  • 23,672
  • 5
  • 40
  • 80