How to find the maximum value in a list of tuples?
Use max()
:
Using itemgetter()
:
In [53]: lis=[(101, 153), (255, 827), (361, 961)]
In [81]: from operator import itemgetter
In [82]: max(lis,key=itemgetter(1))[0] #faster solution
Out[82]: 361
using lambda
:In [54]: max(lis,key=lambda item:item[1])
Out[54]: (361, 961)
In [55]: max(lis,key=lambda item:item[1])[0]
Out[55]: 361
timeit
comparison:In [30]: %timeit max(lis,key=itemgetter(1))
1000 loops, best of 3: 232 us per loop
In [31]: %timeit max(lis,key=lambda item:item[1])
1000 loops, best of 3: 556 us per loop
Return maximum value from list of tuples
Just convert the key field to int
data = [('Frank', '8'), ('Peter', '10'), ('Spank', '0')]
mx = max(data, key=lambda e: int(e[1]))
print(mx)
output('Peter', '10')
Finding the max element's index in a Tuple list? (Python)
If you want only max tuple value:
max_tuple = max(temp_tuple, key=lambda x:x[1])
print(max_tuple)
>> ('B', 3)
if you want index of max tuple too:max_tuple = max(temp_tuple, key=lambda x:x[1])
max_tuple_index = temp_tuple.index(max_tuple)
print(max_tuple)
print(max_tuple_index)
>> ('B', 3)
>> 1
Get a list of maximum values per tuple from a list of tuples
Right now you are just looping through the tuples and returning the "biggest" one - tuple comparison is done element-wise.
What you want is to add another loop level that will find the maximum of each tuple:
def max_element_per_tuple(tuple_list):
res = []
for tup in tuple_list: # loops over the tuples in the list
maximum = tup[0]
for item in tup: # loops over each item of the tuple
if item > maximum:
maximum = item
res.append(maximum)
return res
This gives as expected:>>> max_element_per_tuple([(-1, 0, 1), (10, 20, 30), (100, 50, 25), (55, 75, 65)])
[1, 30, 100, 75]
Min and Max value in list of tuples
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"
Get the max value on a list of tuples
replace
CardWithHighestValue= max(card_list,key=itemgetter(1)[0])
withCardWithHighestValue= max(card_list,key=itemgetter(1))
Demofrom operator import itemgetter
card_list= [(2, (1, "S")), (0, (12, "H")), (1, (5, "C"))]
print max(card_list,key=itemgetter(1))
card_list= [(2, (1, "S")), (0, (4, "H")), (1, (5, "C"))]
print max(card_list,key=itemgetter(1))
Output:(0, (12, 'H'))
(1, (5, 'C'))
Finding minimum and maximum in a list of tuples i.e (min,max),(max,min)?
A possible approach is to identify the maximum and minimum values of x and y, generate all possible tuples with those values, and check if those tuples exist in the list.
coords = [(200,200),(20,200),(200,20),(20,20)]
max_x = max([t[0] for t in coords])
min_x = min([t[0] for t in coords])
max_y = max([t[1] for t in coords])
min_y = min([t[1] for t in coords])
for x in [min_x,max_x]:
for y in [min_y,max_y]:
if (x,y) in coords:
print( (x,y) )
Given your clarifying comments, I think that's sufficient for your needs. If the input data didn't reliably contain the combinations you're looking for, you'd have to be more specific about what you want.For that matter, if you are guaranteed that the combinations you want are in the input data, you could remove the if
line and just output all of the permutations. But it's probably wise to verify they are actually there.
Getting the maximum value of element tuple in list of tuples
If you want the last element, just say so by using the index -1
in your code. In your code, the 1
picks the second element of each tuple for comparison in max
and the 2
is unnecessarily hardcoding the knowledge that your tuples have length three.
>>> vals = [(1,3,5),(2,2,5),(1,2,6)]
>>> max(vals, key=itemgetter(-1))[-1]
>>> 6
Related Topics
Python Pip on Windows - Command 'Cl.Exe' Failed
Python Ignore Certificate Validation Urllib2
How to Strip All Whitespace from String
How to Handle Exceptions in a List Comprehensions
Python, Remove All Non-Alphabet Chars from String
When to Use Get, Get_Queryset, Get_Context_Data in Django
Passing Double Quote Shell Commands in Python to Subprocess.Popen()
Preventing Python Code from Importing Certain Modules
Strip/Trim All Strings of a Dataframe
Best Way to Make Django's Login_Required the Default
How to Change a Widget's Font Style Without Knowing the Widget's Font Family/Size
Change to Sudo User Within a Python Script
How to Convert an Int to a Hex String
Can't Get Python to Import from a Different Folder