Dayo Adewunmi wrote:

It works when I use your example, but I don't understand why it won't work when I use 4-element tuples instead of 2:

What makes you say it doesn't work? It looks like it works to me:

>>>l = [('wascas','aaa','fdvdfv', 1), ('rtgdsf','bbb','trfg', 1), ('addwe','ccc','esd', 1), ('xasd','aaa','wascaq', 2), ('nhy','bbb','asw', 2), ('zzzz','ccc','dgdeg', 2)]
 >>> l
[('wascas', 'aaa', 'fdvdfv', 1), ('rtgdsf', 'bbb', 'trfg', 1), ('addwe', 'ccc', 'esd', 1), ('xasd', 'aaa', 'wascaq', 2), ('nhy', 'bbb', 'asw', 2), ('zzzz', 'ccc', 'dgdeg', 2)]
 >>> l.sort(key=itemgetter(3))
 >>> l
[('wascas', 'aaa', 'fdvdfv', 1), ('rtgdsf', 'bbb', 'trfg', 1), ('addwe', 'ccc', 'esd', 1), ('xasd', 'aaa', 'wascaq', 2), ('nhy', 'bbb', 'asw', 2), ('zzzz', 'ccc', 'dgdeg', 2)]

List "l" is now sorted by the element in position 3 (counting from zero, not one): 1 1 1 2 2 2


 >>> l.sort(key=itemgetter(1))
 >>> l
[('wascas', 'aaa', 'fdvdfv', 1), ('xasd', 'aaa', 'wasca q', 2), ('rtgdsf', 'bbb', 'trfg', 1), ('nhy', 'bbb', '
asw', 2), ('addwe', 'ccc', 'esd', 1), ('zzzz', 'ccc', 'dgdeg', 2)]

And now elements are sorted in order of position 1:
aaa aaa bbb bbb ccc ccc

In the event of ties (and there are three pairs of ties) the elements keep the relative order they were in after the first sort.

The sorting worked just as expected.




--
Steven

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

Reply via email to