简体   繁体   中英

pairwise comparison of integers in list

What I am trying to do here is to compare integers within pairs.

if I have a list of pairs

[(10, 5), (6, 3), (2, 20), (100, 80)]

I would want to compare x > y for each of the pairs and return False if any of the pairs do not meet the condition

def function(list_or_tuple):
num_integers = len(list_or_tuple)

pairs_1 = list(zip(list_or_tuple[::2], list_or_tuple[1::2]))
print(pairs_1)
#pairs_2 = list(zip(list_or_tuple[1::2], list_or_tuple[2::2]))
#print(pairs_2)

for x1, y1 in pairs_1:
    return bool(x1 > y1)

and my program keeps on returning True for the above example

I believe the program is only testing the first pair, which is (10,5)

what should I do to make my program test all the pairs in the list?

It would be much easier to use the all function with a list comprehension:

lst = [(10, 5), (6, 3), (2, 20), (100, 80)]
result = all(x[0] > x[1] for x in lst)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM