繁体   English   中英

列表中整数的成对比较

[英]pairwise comparison of integers in list

我在这里要做的是比较成对中的整数。

如果我有一对配对列表

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

我想比较每个对的x> y,如果任何一对不符合条件,则返回False

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)

并且我的程序继续为上面的例子返回True

我相信该程序只测试第一对,即(10,5)

我该怎么做才能让我的程序测试列表中的所有对?

使用列表推导的all函数会容易得多:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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