from itertools import groupby
from operator import itemgetter
# sid, symbol, rank
data = [
(36, 'abc', 5158),
(35, 'aaa', 4023),
(44, 'aaa', 3756),
(171, 'alc', 3262),
(179, 'soc', 701),
(42, 'abs', 3879),
(43, 'abs', 531),
]
def max_unique_rank(data):
_sid, _symbol, _rank = range(3)
filtered = sorted(data, key=itemgetter(_symbol, _rank), reverse=True)
filtered = groupby(filtered, key=itemgetter(_symbol))
filtered = [next(group) for _, group in filtered]
return filtered
max_unique_rank(data)
Результат:
[(179, 'soc', 701),
(171, 'alc', 3262),
(42, 'abs', 3879),
(36, 'abc', 5158),
(35, 'aaa', 4023)]
Вариант с сохранением порядка и прозрачной логикой:
cache = {}
for sid, symbol, rank in data:
_sid, _rank = cache.get(symbol, (sid, rank))
if _rank <= rank:
cache[symbol] = (sid, rank)
filtered = [(sid, symbol, rank) for symbol, (sid, rank) in cache.items()]
Одной строкой:
{symbol: (sid, symbol, rank) for sid, symbol, rank in sorted(data, key=itemgetter(2))}.values()