我有一个元组列表,我试图为此获取每个索引的最小值和最大值。索引1似乎工作正常。但是,对于索引2,我期望的值为(-2,11),但我得到的却是(5,5)。任何人都可以解释出什么问题了吗?谢谢。
test_list = [(-2, 5), (-1, 5), (0, 6), (0, 7), (0, 7), (1, 8), (2, 9), (2, 9), (3, 10),
(4, 11), (-1, 4), (0, 5), (0, 5), (0, 6), (1, 7), (2, 7), (2, 8), (3, 9), (4, 9), (5, 10),
(0, 3), (0, 4), (0, 5), (1, 5), (2, 6), (2, 7), (3, 7), (4, 8), (5, 9), (5, 9), (0, 2),
(0, 3), (1, 4), (2, 5), (2, 5), (3, 6), (4, 7), (5, 7), (5, 8), (6, 9), (0, 2), (1, 2),
(2, 3), (2, 4), (3, 5), (4, 5), (5, 6), (5, 7), (6, 7), (7, 8), (1, 1), (2, 2), (2, 2),
(3, 3), (4, 4), (5, 5), (5, 5), (6, 6), (7, 7), (7, 7), (2, 0), (2, 1), (3, 2), (4, 2),
(5, 3), (5, 4), (6, 5), (7, 5), (7, 6), (8, 7), (2, 0), (3, 0), (4, 1), (5, 2), (5, 2),
(6, 3), (7, 4), (7, 5), (8, 5), (9, 6), (3, 0), (4, 0), (5, 0), (5, 1), (6, 2), (7, 2),
(7, 3), (8, 4), (9, 5), (9, 5), (4, -1), (5, 0), (5, 0), (6, 0), (7, 1), (7, 2), (8, 2),
(9, 3), (9, 4), (10, 5)]
res1 = max(test_list)[0],min(test_list)[0]
res2 = max(test_list)[1],min(test_list)[1]
print ("The min of index 1 : " + str(res1))
print ("The min of index 2 : " + str(res2))
#The min of index 1 : (10, -2)
#The min of index 2 : (5, 5)
只需尝试以下列表理解:
first_elements = [elem[0] for elem in test_list]
second_elements = [elem[1] for elem in test_list]
print(min(first_elements), max(second_elements))
print(max(first_elements), min(second_elements))
输出:
"-2 11"
"10 -1"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句