1

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. For index 2, however, I'm expecting the value (-2,11), but I get (5, 5) instead. 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)
  • 2
    Think about `max(test_list)[1]`. That's taking the max of all tuples, then getting the second element of the the max tuple. That isn't getting the tuple with the max 1th element. – Carcigenicate Jan 05 '21 at 18:24
  • I started with this article: https://www.geeksforgeeks.org/python-min-and-max-value-in-list-of-tuples/ I'm probably wrong, but it seems like the code is returning tuples of the max element at each index. – Dr. Pontchartrain Jan 05 '21 at 18:26
  • 2
    GeeksForGeeks is hot garbage. Avoid it at all costs. The amount of completely ridiculous advice I've seen on there is astounding. It allows anyone, regardless of skill or knowledge, to post without any checks for validity or correctness. I've actually tried correcting some articles of theirs, and was rejected for "attempting to make changes to existing code"; even the existing code was completely nonsensical. – Carcigenicate Jan 05 '21 at 18:27
  • This is how tuple comparison works in Python https://stackoverflow.com/questions/5292303/how-does-tuple-comparison-work-in-python. However it looks like you're not really looking for the max/min tuple at all, but the max/min first and second elements. – Simon Notley Jan 05 '21 at 18:29
  • 1
    Like, see [here](https://www.geeksforgeeks.org/write-a-function-to-delete-a-linked-list/) (the post I tried to correct). The Python code was clearly written by someone with very little experience in Python. They seem to think that Python is C, and that all memory requires manual `del`etion, which is ridiculous. And then in the comments for the post: "Not only the data, node also has to be deleted and head should reset". No, that's not how any of this works. It's bad advice all the way down. – Carcigenicate Jan 05 '21 at 18:34

3 Answers3

0

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]
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
0

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:

"-2 11"
"10 -1"
ljuk
  • 701
  • 3
  • 12
0

You can simple iterate over the list and get the tuples with a for loop.

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]

Try moving the index [] to the end of test_list like this: 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
Apealed
  • 1,415
  • 8
  • 29