Re: [Tutor] Fwd: Re: Doubt in Python

2019-01-17 Thread Steven D'Aprano
On Thu, Jan 17, 2019 at 09:57:03AM +, 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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Re: Doubt in Python

2019-01-17 Thread Alan Gauld via Tutor


CCing the list. Please use Reply All or Reply List on responses to the list.

On 17/01/2019 07:24, Maninath sahoo wrote:
 a=[100,50,30]
 b=[100,90,3]
 a True
 a>b
> False
>
>
> How it compares between two lists
>
The algorithm is probably described somewhere in the documentation
but my understanding is that it looks something like this(in pdeudo code):

def isEqual(X,Y):

    if len(X) != len(Y): return False

    if X[0] != Y[0]: return False

    return X[1:] == Y[1:]


def isGreater(X,Y):

   if len(X) <= len(Y) return False

   if X[0] < Y[0] return False

   if X[0] == Y[0] return X[1:] > Y[1:]

   return True


And similarly for the other operators.

But that's just based on playing with the interpreter and exploring
different values...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor