On 20/01/14 01:16, Doug and Riekie Dorman wrote:
I think I may have found a bug:

pairs  =  [(1,  'one'),  (2,  'two'),  (3,  'three'),  (4,  'four')]
pairs.sort(key=lambda  pair:  pair[1])
pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]

Should be:

pairs  =  [(1,  'one'),  (2,  'two'),  (3,  'three'),  (4,  'four')]
pairs.sort(key=lambda  pairs:  pairs[1])
pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]

I have increased the size of the two s's to show the problem.

In plain text that doesn't help. However you seem to be confused about what the lambda is doing.

pairs.sort(key = lambda p : p[1])

is the same as

def somefunc(p):
    return p[1]

pairs.sort(key=somefunc)

The parameter to the lambda function is completely separate to the list. It's part of the function definition. It takes on whatever
value is passed, in your case it is passed the individual tuples
within the list. The name you use is irrelevant.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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

Reply via email to