Python Forum
using > < for tuple , list,...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using > < for tuple , list,...
#1
hi
I saw on the net that in a test, two tuples were compared with the > sign.
how can I know that the > or < or ... is applicable for a type such as tuples or lists or dicst,...
thanks
Reply
#2
The best way to learn, is to explore the concept. Here...
t1 = ("A", "B", "C")

t2 = ("A", "B", "D")
... t2 > t1 because t2 has a "D" as one of its items.

And here...

t1 = ("A", "B", "C")

t2 = ("B", "A", "C")
... t2 > t1 because "B" is the first item in t2 and "B" > "A".
akbarza likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
From documentation (Value Comparisons):

Quote:Sequences compare lexicographically using comparison of corresponding elements.

This means that order is important. This makes sense in strings (this is the way how words are ordered in any dictionary) but with tuples and lists one must be aware what practical implications of lexicographical comparison have on greater or less:

>>> a = (2, 1)
>>> b = (1, 4, 5, 10)
>>> a < b
False
1 is smaller than 2 and so b is smaller than a.
akbarza likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
You can tell if an object can do comparison by looking to see if it has comparison dunder methods.
Output:
>>> x = (1, 2) >>> dir(x) ['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index'] >>>
Here I see that the tuple x can do __eq__, __ge__, __gt__, __le__, __lt__ and __ne__ which correspond to the comparison operators =, >=, >, <=, <, !=. You still have to read the documentation for the class, because this only tells you that comparison is supported, not how the comparison is done.

https://docs.python.org/3/tutorial/datastructures.html

Quote:5.8. Comparing Sequences and Other Types
Sequence objects typically may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. If two items to be compared are themselves sequences of the same type, the lexicographical comparison is carried out recursively. If all items of two sequences compare equal, the sequences are considered equal. If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one. Lexicographical ordering for strings uses the Unicode code point number to order individual characters. Some examples of comparisons between sequences of the same type:

For lists an tuples, the items in the list or tuple must also be comparable.
x = ('A', 'B', 'C')
y = (1, 2, 3)
print(x > y)
Error:
print(x > y) ^^^^^ TypeError: '>' not supported between instances of 'str' and 'int'
akbarza likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 496 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Change font in a list or tuple apffal 4 2,704 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  search a list or tuple for a specific type ot class Skaperen 8 1,954 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  why is my list a tuple CompleteNewb 7 2,283 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb
  in a list or tuple Skaperen 6 93,374 May-16-2021, 09:59 PM
Last Post: Skaperen
  Create SQLite columns from a list or tuple? snakes 6 8,742 May-04-2021, 12:06 PM
Last Post: snakes
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,402 Jan-30-2021, 07:11 AM
Last Post: alloydog
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,849 Nov-04-2020, 11:26 AM
Last Post: Aggam
  Python Error- TypeError: ('Params must be in a list, tuple, or Row', 'HY000') DarkCoder2020 3 5,609 Jul-29-2020, 12:02 AM
Last Post: Larz60+
  Arrange list of tuple using loop batchenr 7 3,502 Jun-16-2019, 03:24 PM
Last Post: Abdullah

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020