Reputation: 4924
Given these two lists:
first = [('-2.50', '1.91', '2.03'), ('3.00', '1.83', '2.08')]
second = [(('-2.50', 0.889258, 1.069258), ('3.00', 0.931381, 1.021381))]
It's a two-task challenge. Firslty, in list second
, I need to identify a tuple with greatest value in it (while values at position 0
here: -2.50
and 3.00
must be ignored). Then, as a second task, we need to output the corresponding tuple form list first
. So it should result in:
('-2.50', '1.91', '2.03')
This is because the greatest value found in step first should be 1.069258
which is inside 1st tuple.
The snag I face here is finding the tuple with greatest value (I know I can use max()
to find the value but I need the whole tuple), with the 2nd part of the problem I think I'll cope simply using the if
statement.
Upvotes: 0
Views: 131
Reputation: 250931
One liner:
>>> first [ max(enumerate(second[0]), key=lambda x: max(x[1][1:] ))[0] ]
('-2.50', '1.91', '2.03')
or:
>>> maxx = float("-inf")
>>> for i,x in enumerate(second[0]):
... temp = max(x[1:])
... if temp > maxx:
... index = i
... maxx = temp
...
>>> first[i]
('-2.50', '1.91', '2.03')
Upvotes: 1
Reputation: 945
You want to use a key function to compute the value by which each tuple is compared. You want to compare the tuples by the largest value other than the first, so try
>>> max(second[0], key = lambda x: max(x[1:]))
('-2.50', 0.889258, 1.069258)
Upvotes: 0
Reputation: 309899
In 1 line:
>>> max(zip(first,second[0]),key=lambda x:max(x[1][1:]))[0]
('-2.50', '1.91', '2.03')
Upvotes: 5