Python – 测试是否大于元组列表中的前一个元素
给定元组列表,检查元组列表中每个元素的前一个元素是否小于当前元素。
Input : test_list = [(5, 1), (4, 9), (3, 5)]
Output : [[False, False], [False, True], [False, True]]
Explanation : First element always being False, Next element is checked for greater value.
Input : test_list = [(1, 8), (2, 2), (3, 6), (4, 2)]
Output : [[False, True], [False, False], [False, True], [False, False]]
Explanation : 8 and 6 are greater cases in above cases, hence True.
方法 #1:使用列表理解 + enumerate()
上述功能的组合可以用来解决这个问题。在此,我们使用一个线性列表理解来执行检查更大值的任务,并且 enumerate() 用于在嵌套迭代时处理索引。
# Python3 code to demonstrate working of
# Test if greater than preceding element in Tuple List
# Using list comprehension + enumerate()
# initializing list
test_list = [(3, 5, 1), (7, 4, 9), (1, 3, 5)]
# printing original list
print("The original list : " + str(test_list))
# Test if greater than preceding element in Tuple List
# Indices checked using enumerate() and True and false
# values assigned in list comprehension
res = [[True if idx > 0 and j > i[idx - 1] else False
for idx, j in enumerate(i)] for i in test_list]
# printing result
print("Filtered values : " + str(res))
输出 :
The original list : [(3, 5, 1), (7, 4, 9), (1, 3, 5)]
Filtered values : [[False, True, False], [False, False, True], [False, True, True]]
方法 #2:使用tee() + zip()
+ 列表理解
这是可以执行此任务的方式之一。在此,我们使用 tee() 提取元素并将它们呈现在大小 = 2 的元组中。列表理解和 zip() 用于构造所需的结果。
# Python3 code to demonstrate working of
# Test if greater than preceding element in Tuple List
# Using tee() + zip() + list comprehension
from itertools import tee
# helper function
def pair(test_list):
# pairing elements in 2 sized tuple
x, y = tee(test_list)
next(y, None)
return zip(x, y)
# initializing list
test_list = [(3, 5, 1), (7, 4, 9), (1, 3, 5)]
# printing original list
print("The original list : " + str(test_list))
# Test if greater than preceding element in Tuple List
# Using tee() + zip() + list comprehension
res = []
for sub in test_list:
# appending result by checking with Dual Pairs
res.append(tuple((False, )) + tuple([x < y for x, y in pair(sub)]))
# printing result
print("Filtered values : " + str(res))
输出 :
The original list : [(3, 5, 1), (7, 4, 9), (1, 3, 5)]
Filtered values : [[False, True, False], [False, False, True], [False, True, True]]