簡體   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