A compact one-pass solution requires sorting the list — technically O(N log N)for a Nlong list, but Python sorting is so good, and so many sequences “just happen” to have a built-in order in them (which they timsortskillfully use to go faster) that sort-based solutions sometimes have amazingly good performance in the real world.
Solution 2.6 or better is required here:
import itertools
import operator
f = operator.itemgetter(0)
def minima(lol):
return list(next(itertools.groupby(sorted(lol, key=f), key=f))[1])
To understand this approach, an inside-out search helps.
f, i.e. operator.itemgetter(0), is a key function that selects the first element of its argument for sequencing purposes. The goal itself operator.itemgetteris to create such functions easily and compactly.
sorted(lol, key=f), , - lol, . key=f, , , " " - .. - key=f . , ( , ), , .
itertools.groupby(sorted(lol, key=f), key=f) "", : ( sorted ) key. , , , f , , ( ), . groupby , , lol ( groupby , ).
yield ed by groupby k, g: k, f(i) , g, .
next ( , Python 2.6), , - , , , ( , , , groupby). Python groupby(...).next() ( next , ), 2.6.
, , next(...) k, g, k - ( ) , g - .
, [1] , , .
, ( ), list(...) .
, ? , - minima , @Kenny ( , "" ). , , , ( , , , c, & c; -).