1

Suppose two tuples are having 2 elements in both of them.

t1=(2,8)

t2=(5,6)

and suppose I want to compare both the tuple on the basis of the first element of both of them. So, when we do t1>t2 then the output should be True (as t1[1]>t2[1]).

so I want to make a comparison between these two tuples on the basis of first element.

I know that I have to override the compare function of tuple. But I don't know how to do it.

So please tell me how can I do that? (Suppose I want to compare the elements in ascending order...)

The reason I want to do that is : Merging k sorted lists using heapq module in python3

I am using Python-3.8.5

Abhishek Jaiswal
  • 288
  • 2
  • 14
  • what do you mean by comparison greater than lower than what do you want to compare – bunyaminkirmizi May 03 '21 at 08:00
  • What if the first element is equal between the tuples? – Tomerikoo May 03 '21 at 08:05
  • see the post again, now, it is mentioned... – Abhishek Jaiswal May 03 '21 at 08:08
  • What you want is already the built-in behavior... Did you try `t1 > t2`? – Tomerikoo May 03 '21 at 08:09
  • @Tomerikoo Yes I tried but when I do t1>t2, only the first element is compared. – Abhishek Jaiswal May 03 '21 at 08:12
  • ....Which is exactly what you said you want.... The first element is compared, if it is equal, the second element will be compared – Tomerikoo May 03 '21 at 08:15
  • But what should I do if I want to compare the second element rather than comparing the first element? – Abhishek Jaiswal May 03 '21 at 08:16
  • That is not what you asked in your question and you already got mis-leaded answers. Please clarify your question with examples what is exactly the behavior you're looking for – Tomerikoo May 03 '21 at 08:17
  • @Tomerikoo Thanks for your suggestion,i have edited my question, if there is still something to be edited then please edit this question on your behalf... It would be a great help to me... – Abhishek Jaiswal May 03 '21 at 08:27
  • 1
    You can easily use my answer to make the comparison whatever you want it to be. The total_ordering generates all the other comparison operators using the __eq__ and __lt__ you provide. This is 'overriding' the compare function of the tuple in your own subclass of tuple. – Paul Whipp May 03 '21 at 20:41

2 Answers2

1

You can simply get the first element of the tuple and compare.

t1 = (1,2)
t2 = (5,4)
print(t1[0] > t2[0])

or if you want to filter or sort.

sorted_tuples = sorted(list_of_tuples, key= lambda x: x[0])
Ceres
  • 2,498
  • 1
  • 10
  • 28
1

Here's the 'classy' way ;):

In [1]: from functools import total_ordering


In [3]: @total_ordering
   ...: class FirstItemComparisonTuple(tuple):
   ...:     def __eq__(self, other):
   ...:         return self[0] == other[0]
   ...:     def __lt__(self, other):
   ...:         return self[0] < other[0]
   ...: 


In [5]: a = FirstItemComparisonTuple((1, 2))

In [6]: a[0]
Out[6]: 1

In [7]: b = FirstItemComparisonTuple((2, 4))

In [8]: a < b
Out[8]: True
Paul Whipp
  • 16,028
  • 4
  • 42
  • 54