Finding if a value in a list is greater than the item below it

Viewed 3966

I've looked for a solution for this for a little while, and while i have snippets of understanding, i can't quite get it to work as expected. I'm hoping i could get some insight:

So i have a list that contains numbers:

refl = ["100", "99", "90", "80", "60", "50", "10"]

I want to find out if the first item is larger than the second, if the second is larger than the third, the third larger than the fourth etc.

I guess i'm struggling with how to capture the initial list object to compare it to the next...?

Any help would be greatly appreciated,

****EDIT TO ADD FUNCTION*****

I have the following function:

refl = ["100", "99", "90", "80", "60", "50", "10"]

def funcc(refl):
    if (refl[0]) > (refl[1]):
        print("more")
    else:
        print("less")

How would i get the function to run through each object in the list without implicitly specifying if [1] > [2], [2] > [3] etc

Many Thanks,

5 Answers

You could zip() the list into pairs, then check if the first item from each pair is greater than the second item. I'm assuming you want integer comparisons here. If not, you can remove the int() casts, which will compare the strings lexicographically using their respective ASCII values.

You could then use all() to check if every pair satisfies this condition.

>>> refl = ["100", "99", "90", "80", "60", "50", "10"]
>>> all(int(fst) > int(snd) for fst, snd in zip(refl, refl[1:]))
True

If you just want to capture the boolean result from each comparison, you can use a list comprehension:

>>> [int(fst) > int(snd) for fst, snd in zip(refl, refl[1:])]
[True, True, True, True, True, True]

This is your existing code:

def funcc(refl):
    if (refl[0]) > (refl[1]):
        print("more") 
    else:
        print("less")

Your problem is, you only compare the first (refl[0]) and second (refl[1]) elements. A trivial fix would be:

def funcc(refl):
    for i in range(len(refl)) - 1:
        if (refl[i + 1]) >= (refl[i]):
            return False
    return True

then use it as follows:

refl = ["100", "99", "90", "80", "60", "50", "10"]
if funcc(refl):
    print("Monotone decreasing")
else:
    print("Not monotone decreasing")

Your variable refl is a list, and you can reference each item in a list using an integer index. You can use those indices to compare values and a while loop to automatically compare each pair of items. To do that, you first would need to configure your loop to visit each item in the list, stopping one short of the last item since you will be comparing one item ahead.

refl = ["100", "99", "90", "80", "60", "50", "10"]
counter = 0
while counter < len(refl)-1: #remember, the length of the list is 7, the last index is 6
    if refl[counter] < refl[counter+1]: #if the item smaller than the next item
        print("The previous item is not larger")
    else:
        print("the previous item is larger")
    counter += 1 #add one to counter and re-assign

If the comparison is for integer, you need this first to convert all items into integer.

refl = list(map(int, refl))

then, logical AND all for comparison greater first n-1 items with last n-1 items.

result = all(map(int.__gt__, refl[:-1], refl[1:]))

refl = ["100", "99", "90", "80", "60", "50", "10"]

def procedure(refl): 
    for count in range(len(ref1)):
        if (refl[count]) > (refl[count+1]): 
            print("more") 
        else: 
            print("less")
procedure(ref1)

len(ref1) will repeat the loop n times where n is the number of items in # #the list.

Related