Sorting a set works, sorting a dictionary fails ?

Fábio Santos fabiosantosart at gmail.com
Mon Jun 10 05:27:14 EDT 2013


On 10 Jun 2013 09:34, "Νικόλαος Κούρας" <nikos.gr33k at gmail.com> wrote:
>
> Trying this:
>
> months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4,
'Μάϊος':5, 'Ιούνιος':6, \
>            'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10,
'Νοέμβριος':11, 'Δεκέμβριος':12 }
>
> for key in sorted( months.values() ):
>         print('''
>                 <option value="%s"> %s </option>
>         ''' % (months[key], key) )
>
>
> output this:
>
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173]   File
"/home/nikos/public_html/cgi-bin/pelatologio.py", line 310, in <module>,
referer: http://superhost.gr/
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173]     ''' %
(months[key], key) ), referer: http://superhost.gr/
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] KeyError: 1,
referer: http://superhost.gr/
>
> KeyError 1 ??!! All i did was to tell python to sort the dictionary
values, which are just integers.
> --
> http://mail.python.org/mailman/listinfo/python-list

KeyError: 1 means that there is no int(1) key. I think you meant to do "for
key in sorted(yourdict.keys())"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130610/a3c24d22/attachment.html>


More information about the Python-list mailing list