简体   繁体   中英

Getting the correct max value from a list of tuples

My list of tuples look like this:

[(0, 0), (3, 0), (3, 3), (0, 3), (0, 0), (0, 6), (3, 6), (3, 9), (0, 9), (0, 6), (6, 0), (9, 0), (9, 3), (6, 3), (6, 0), (0, 3), (3, 3), (3, 6), (0, 6), (0, 3)]

It has the format of (X, Y) where I want to get the max and min of all Xs and Ys in this list.

It should be min(X)=0, max(X)=9, min(Y)=0, max(Y)=9

However, when I do this:

min(listoftuples)[0], max(listoftuples)[0]
min(listoftuples)[1], max(listoftuples)[1]

...for the Y values, the maximum value shown is 3 which is incorrect.

Why is that?

for the Y values, the maximum value shown is 3

because max(listoftuples) returns the tuple (9, 3) , so max(listoftuples)[0] is 9 and max(listoftuples)[1] is 3 .

By default, iterables are sorted/compared based on the values of the first index, then the value of the second index, and so on.

If you want to find the tuple with the maximum value in the second index, you need to use key function:

from operator import itemgetter

li = [(0, 0), (3, 0), ... ]
print(max(li, key=itemgetter(1)))
# or max(li, key=lambda t: t[1])

outputs

(3, 9)

Here is a simple way to do it using list comprehensions:

min([arr[i][0] for i in range(len(arr))])
max([arr[i][0] for i in range(len(arr))])  
min([arr[i][1] for i in range(len(arr))])
max([arr[i][1] for i in range(len(arr))]) 

In this code, I have used a list comprehension to create a list of all X and all Y values and then found the min/max for each list. This produces your desired answer.

The first two lines are for the X values and the last two lines are for the Y values.

Tuples are ordered by their first value, then in case of a tie, by their second value (and so on). That means max(listoftuples) is (9, 3) . See How does tuple comparison work in Python?

So to find the highest y-value, you have to look specifically at the second elements of the tuples. One way you could do that is by splitting the list into x-values and y-values, like this:

xs, ys = zip(*listoftuples)

Or if you find that confusing, you could use this instead, which is roughly equivalent:

xs, ys = ([t[i] for t in listoftuples] for i in range(2))

Then get each of their mins and maxes, like this:

x_min_max, y_min_max = [(min(L), max(L)) for L in (xs, ys)]
print(x_min_max, y_min_max)  # -> (0, 9) (0, 9)

Another way is to use NumPy to treat listoftuples as a matrix.

import numpy as np

a = np.array(listoftuples)
x_min_max, y_min_max = [(min(column), max(column)) for column in a.T]
print(x_min_max, y_min_max)  # -> (0, 9) (0, 9)

(There's probably a more idiomatic way to do this, but I'm not super familiar with NumPy.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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