[Tutor] Fwd: Re: Doubt in Python
Steven D'Aprano
steve at pearwood.info
Thu Jan 17 10:28:12 EST 2019
On Thu, Jan 17, 2019 at 09:57:03AM +0000, Alan Gauld via Tutor wrote:
> The algorithm is probably described somewhere in the documentation
> but my understanding is that it looks something like this(in pdeudo code):
List, tuple and string comparisons are defined as lexicographical order:
http://docs.python.org/tutorial/datastructures.html#comparing-sequences-and-other-types
https://en.wikipedia.org/wiki/Lexicographical_order
That's a fancy way of saying "dictionary order". That means that lists
are ordered in the same way that words are ordered in the dictionary:
- match up letters in the word (items in the list) in pairs;
- so long as the pairs of letters (items) are equal, keep going;
- as soon as you hit a pair that aren't equal, the order of that pair
determines the order of the words (lists);
- if one word runs out of letters (list runs out of items), then it
comes first;
- if all the pairs are equal, the words (lists) are equal.
Some examples:
[0, 1, 2] < [1, 2, 3] because 0 < 1
[0, 1, 2] < [0, 1, 2, 3] because the first three items are equal
but the first list is shorter.
[0, 1, 2, 3, 4, 5] < [0, 1, 999] because 2 < 999
[0, 999, 999, 999] < [1, 2] because 0 < 1
--
Steve
More information about the Tutor
mailing list