For a given list of tuples, if multiple tuples in the list have the first element of tuple the same - among them select only the tuple with the maximum last element.
For example:
sample_list = [(5,16,2),(5,10,3),(5,8,1),(21,24,1)]
In the sample_list above since the first 3 tuples has the similar first element 5 in this case among them only the 2nd tuple should be retained since it has the max last element => 3.
Expected op:
op = [(5,10,3),(21,24,1)]
Code:
op = []
for m in range(len(sample_list)):
li = [sample_list[m]]
for n in range(len(sample_list)):
if(sample_list[m][0] == sample_list[n][0]
and sample_list[m][2] != sample_list[n][2]):
li.append(sample_list[n])
op.append(sorted(li,key=lambda dd:dd[2],reverse=True)[0])
print (list(set(op)))
This works. But it is very slow for long list. Is there a more pythonic or efficient way to do this?
Try itertools.groupby:
from itertools import groupby
sample_list.sort()
print([max(l, key=lambda x: x[-1]) for _, l in groupby(sample_list, key=lambda x: x[0])])
Or also with operator.itemgetter:
from itertools import groupby
from operator import itemgetter
sample_list.sort()
print([max(l, key=itemgetter(-1)) for _, l in groupby(sample_list, key=itemgetter(0))])
For performance try:
from operator import itemgetter
dct = {}
for i in sample_list:
if i[0] in dct:
dct[i[0]].append(i)
else:
dct[i[0]] = [i]
print([max(v, key=itemgetter(-1)) for v in dct.values()])
All output:
[(5, 10, 3), (21, 24, 1)]
Use itertools.groupby and operator.itemgetter for readability. Within the groups, apply max with an appropriate key function, again using itemgetter for brevity:
from itertools import groupby
from operator import itemgetter as ig
lst = [(5, 10, 3), (21, 24, 1), (5, 8, 1), (5, 16, 2)]
[max(g, key=ig(-1)) for _, g in groupby(sorted(lst), key=ig(0))]
# [(5, 10, 3), (21, 24, 1)]
For a linear-time solution, with extra-space only bound the number of unique first elements, you may use a dict:
d = {}
for tpl in lst:
first, *_, last = tpl
if first not in d or last > d[first][-1]:
d[first] = tpl
[*d.values()]
# [(5, 10, 3), (21, 24, 1)]
Here is a linear-time method which I think qualifies as more Pythonic:
highest = dict()
for a, b, c in sample_list:
if a not in highest or c >= highest[a][2]:
highest[a] = (a, b, c)
op = list(highest.values())
You can change the >= to > if you care about how to choose between triples with the same first and last elements but different middle elements.
As pointed out by @AlexWaygood, dicts have yielded their elements according to insertion order since Python 3.7. The code above therefore causes the elements of op to be in the same order the elements of sample_list.
In Python 3.6 or older, on the other hand, the order may change. If you want a solution that works in Python 3.6 too, you will need to use an OrderedDict, as in:
from collections import OrderedDict
highest = OrderedDict()
for a, b, c in sample_list:
if a not in highest or c >= highest[a][2]:
highest[a] = (a, b, c)
op = list(highest.values())