Home > Net >  How to compare a pair of values in python, to see if the next value in pair is greater than the prev
How to compare a pair of values in python, to see if the next value in pair is greater than the prev

Time:04-11

I have the following list:

enter image description here

each pair of the value gives me information about a specific row. I want to be able to compare the values from the first position, to the next and see if the next value is less than the current value, if it is keep it that way, if not delete that pair. So for example, for the first index 0 and 1, comparing 29 to 25, I see that 25 is less than 29 so I keep the pair, now I add two to the current index, taking me to 16 here I see that 16 is not less than 19 so I delete the pair values(16,19). I have the following code:

curr = 0
skip = 0
finapS = []

while curr < len(apS):
    if distance1[apS[skip 1]] < distance1[apS[skip]]:
        print("its less than prev")
        print(curr,skip)
        finapS.append(distance1[apS[skip]])
        finapS.append(distance1[apS[skip 1]])
        skip = skip   2
        curr = curr   1
        print("itterated,", skip, curr)

distance1 is a list of values that has variations of data points. apS is a list that contains the index of the important values from the distance1 list. Distance1 has all the values, but I need only the values from the index of apS, now I need to see if those pairs and the values of them are in descending order. The Code I tried running is giving me infinite loop I can't understand why. Here I am adding the values to a new list, but if possible I would like to just delete those pairs of value and keep the original list.

CodePudding user response:

If your test is false you loop without augmenting the counter curr.
You need an

else: 
  curr =1

(or =2 according to the logic)
to progress through the list.

CodePudding user response:

I think this kind of logic is more easily done using a generator. You can loop through the data and only yield values if they meet your condition. e.g.

def filter_pairs(data):
    try:
        it = iter(data)
        while True:
            a, b = next(it), next(it)
            if b < a:
                yield from (a, b)
    except StopIteration:
        pass

Example usage:

>>> aps = [1, 2, 3, 1, 2, 4, 6, 5]
>>> finaps = list(filter_pairs(aps))
>>> finaps
[3, 1, 6, 5]

CodePudding user response:

Pure Python speaking, I think zip is the elegant way of doing this, combined with slice steps.

Assuming you list as defined as::

>>> a = [29, 25, 16, 19, 14, 12, 22, 8, 26, 25, 26]

You can zip the list into itself with a shift of one, and a slice step of two::

>>> list(zip(a[:-1], a[1:]))
[(29, 25), (25, 16), (16, 19), (19, 14), (14, 12), (12, 22), (22, 8), (8, 26), (26, 25), (25, 26)]

Once you have that, you can then filter the sequence down to the items you want, your full solution will be::

>>> list((x, y) for (x, y) in zip(a[:-1:2], a[1::2]) if x > y)
[(29, 25), (14, 12), (22, 8), (26, 25)]

If you prefer to go the numpy path, then read about the np.shift function.

  • Related