简体   繁体   English

元组列表中的最小值和最大值

[英]Min and Max value in list of tuples

I have a list of tuples for which I'm trying to get the min & max values at each index.我有一个元组列表,我试图获取每个索引处的最小值和最大值。 Index 1 seems to be working correctly.索引 1 似乎工作正常。 For index 2, however, I'm expecting the value (-2,11), but I get (5, 5) instead.但是,对于索引 2,我期望值为 (-2,11),但我得到的是 (5, 5)。 Can anyone explain what's going wrong please?谁能解释一下出了什么问题? Thank you.谢谢你。

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)

To get the min and max of each element, use list comprehension like so:要获取每个元素的最小值和最大值,请使用列表推导,如下所示:

test_list_min = [min(y[x] for y in test_list) for x in range(len(test_list[0]))]
test_list_max = [max(y[x] for y in test_list) for x in range(len(test_list[0]))]

print(test_list_min)
# [-2, -1]

print(test_list_max)
# [10, 11]

Just try these list comprehensions:试试这些列表推导:

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))

Output: Output:

"-2 11"
"10 -1"

You can simple iterate over the list and get the tuples with a for loop.您可以简单地遍历列表并使用for循环获取元组。

for t in (test_list):
    print("Max: ", max(t), "\tMin:", min(t))

Results:结果:

Max:  5     Min: -2
Max:  5     Min: -1
Max:  6     Min: 0
Max:  7     Min: 0
Max:  7     Min: 0
Max:  8     Min: 1
...

You are reading an index of your max and min results with the line of code: max(test_list)[0],min(test_list)[0]您正在使用以下代码行读取maxmin结果的索引: max(test_list)[0],min(test_list)[0]

Try moving the index [] to the end of test_list like this: max(test_list[3]),min(test_list[3]) .尝试将索引[]移动到test_list的末尾,如下所示: max(test_list[3]),min(test_list[3])


You code should look like this:您的代码应如下所示:

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)]

max1, min1 = max(test_list[0]), min(test_list[0])  
max2, min2 = max(test_list[1]), min(test_list[1])

print ("The min of index 1 :  " +  str(min1))
print ("The min of index 2 :  " +  str(min2))

print ("The max of index 1 :  " +  str(max1))
print ("The max of index 2 :  " +  str(max2))

Results:结果:

The min of index 1 :  -2
The min of index 2 :  -1
The max of index 1 :  5
The max of index 2 :  5

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

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