Python – 有序元组提取
给定一个元组列表,获取所有按升序排序的元组。
Input : test_list = [(5, 4, 6, 2, 4), (3, 4, 6), (2, 5, 6), (9, 1)]
Output : [(3, 4, 6), (2, 5, 6)]
Explanation : Sorted tuples are extracted.
Input : test_list = [(5, 4, 6, 2, 4), (3, 4, 1), (2, 5, 4), (9, 1)]
Output : []
Explanation : No Sorted tuples.
方法 #1:使用列表理解 + sorted()
在此,我们检查元组是否使用 sorted() 进行排序,并且列表推导用于迭代每个元组。
Python3
# Python3 code to demonstrate working of
# Ordered tuples extraction
# Using list comprehension + sorted()
# initializing list
test_list = [(5, 4, 6, 2, 4), (3, 4, 6), (9, 10, 34), (2, 5, 6), (9, 1)]
# printing original list
print("The original list is : " + str(test_list))
# sorted() used to order, comparison operator to test
res = [sub for sub in test_list if tuple(sorted(sub)) == sub]
# printing result
print("Ordered Tuples : " + str(res))
Python3
# Python3 code to demonstrate working of
# Ordered tuples extraction
# Using filter() + lambda + sorted()
# initializing list
test_list = [(5, 4, 6, 2, 4), (3, 4, 6), (9, 10, 34), (2, 5, 6), (9, 1)]
# printing original list
print("The original list is : " + str(test_list))
# sorted() used to order, comparison operator to test
res = list(filter(lambda sub: tuple(sorted(sub)) == sub, test_list))
# printing result
print("Ordered Tuples : " + str(res))
输出
The original list is : [(5, 4, 6, 2, 4), (3, 4, 6), (9, 10, 34), (2, 5, 6), (9, 1)]
Ordered Tuples : [(3, 4, 6), (9, 10, 34), (2, 5, 6)]
方法 #2:使用 filter() + lambda + sorted()
在这种情况下,过滤任务是使用 filter() 完成的,sorted() 馈送到 lambda 进行比较以获得所需的结果。
Python3
# Python3 code to demonstrate working of
# Ordered tuples extraction
# Using filter() + lambda + sorted()
# initializing list
test_list = [(5, 4, 6, 2, 4), (3, 4, 6), (9, 10, 34), (2, 5, 6), (9, 1)]
# printing original list
print("The original list is : " + str(test_list))
# sorted() used to order, comparison operator to test
res = list(filter(lambda sub: tuple(sorted(sub)) == sub, test_list))
# printing result
print("Ordered Tuples : " + str(res))
输出
The original list is : [(5, 4, 6, 2, 4), (3, 4, 6), (9, 10, 34), (2, 5, 6), (9, 1)]
Ordered Tuples : [(3, 4, 6), (9, 10, 34), (2, 5, 6)]