samedi 25 août 2018

(Python) Get tuple with largest numeric element, and if a tie occurs, randomly pick one of the tied tuples

I have a list of tuples:

a = [([4, 7, 9], [3], 5.5), ([2, 5, 8], [3], 5.5), ([2, 5, 8], [9], 4.5)]

and I will like to get the tuple with the largest numeric element (non-list, for e.g. 5.5 in the case of ([4, 7, 9], [3], 5.5), and if there is a tie, say for this example ([4, 7, 9], [3], 5.5) and ([2, 5, 8], [3], 5.5), one of these tied tuples will be randomly selected as the output.

My progress so far:

>>> import operator
>>> max(a, key = operator.itemgetter(1))
([3], [4, 7, 9], 5.5)

I am unsure as to how I can randomly pick one of the tied tuples at this point in time.. Some insight will be great!




Aucun commentaire:

Enregistrer un commentaire