Oct-17-2018, 08:16 PM
(Oct-17-2018, 08:03 PM)nilamo Wrote:>>> def n_max(seq, n=2): ... max_items = sorted(enumerate(seq), key=lambda x: x[1])[-1 * n:] ... return max_items[::-1] ... >>> items = [4, 5, 3, 2, 63, 0, 1, 5] >>> n_max(items, 2) [(4, 63), (7, 5)]....
from operator import itemgetter def n_max(seq, n=2): return sorted(enumerate(seq), key=itemgetter(1), reverse=True)[:n]
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.