Hi im looking to return the key with the highest value, and if they all have the same value to return key less than the others. ie [('iris', 2), ('rose', 6)]
should return rose
and [('iris', 4), ('rose', 4)]
should return iris
.
I have already sorted the dictionary so iris would come before rose, but not sure how to return rose in situations where it is more than iris.
Edit: return sorted(dict), max(dict, key=lambda k: output[k])
gives: (['iris', 'rose'], 'rose') no matter if iris' value is higher than rose, how could I fix this
Use max function with key parameter. key is an anonymous function in this case:
max(a, key=lambda x: x[1])[0]
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.