[Tutor] __getitem__ another problem
Alan Gauld
alan.gauld at yahoo.co.uk
Wed Nov 23 05:09:46 EST 2016
On 23/11/16 06:26, monikajg at netzero.net wrote:
> I do not understand how numbermap.__getitem__ brings back month's key.
numbermap returns the integer corresponding to the key.
That number is then used by sorted as the basis for
sorting month. So for the first entry sorted receives
the value 1, for the second it gets 2. and so on.
It then prints the keys corresponding to those
values.
> month = dict(one='January',
> two='February',
> three='March',
> four='April',
> five='May')
> numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
> sorted(month, key=numbermap.__getitem__)
> ['one', 'two', 'three', 'four', 'five']
--
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
More information about the Tutor
mailing list