Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47627
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Sorting a set works, sorting a dictionary fails ? |
| Date | 2013-06-11 02:14 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <kp616n$nrp$2@dont-email.me> (permalink) |
| References | (1 earlier) <ce76b1a2-62d0-481d-b045-4bc791a84e37@googlegroups.com> <a609b6e4-b5a5-4d72-b5e1-c7271efe8a72@googlegroups.com> <c2eb322d-ac51-4e6a-aa56-3b6b8267ebc4@googlegroups.com> <igjg8a-k5b.ln1@satorlaser.homedns.org> <1b80e978-4e80-4495-88a6-74f4160bab9e@googlegroups.com> |
On Mon, 10 Jun 2013 03:42:38 -0700, Νικόλαος Κούρας wrote:
> for key in sorted( months.values() ):
> print('''
> <option value="%s"> %s </option>
> ''' % (months[key], key) )
> ==============
>
> please tell me Uli why this dont work as expected to.
Because inside the for loop, your value 'key' is an item from the sorted
list of the values part of months. When you use months[key], you're
trying to look up in months based on the value part, not the key part.
Calling it key is confusing you.
Try this:
things = { "a":1, "b":3, "c":2 }
for thing in sorted( things.values() ):
print thing
>> Prints 1, 2, 3
for thing in sorted( things.keys() ):
print thing
>> Prints a, b, c
Now although things["b"] is a valid reference because "b" is a key in a
key - value pair, things[3] is not a valid reference, because 3 is not a
key in a key - value pair.
--
Denis McMahon, denismfmcmahon@gmail.com
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:04 -0700
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:16 -0700
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:18 -0700
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:29 -0700
Re: Sorting a set works, sorting a dictionary fails ? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-10 10:27 +0100
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 02:32 -0700
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 02:48 -0700
Re: Sorting a set works, sorting a dictionary fails ? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-10 11:13 +0100
Re: Sorting a set works, sorting a dictionary fails ? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-10 15:11 +0200
Re: Sorting a set works, sorting a dictionary fails ? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-10 11:40 +0200
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 03:20 -0700
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 03:42 -0700
Re: Sorting a set works, sorting a dictionary fails ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-10 11:47 +0000
Re: Sorting a set works, sorting a dictionary fails ? Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-11 02:14 +0000
Re: Sorting a set works, sorting a dictionary fails ? Larry Hudson <orgnut@yahoo.com> - 2013-06-11 02:16 -0700
Re: Sorting a set works, sorting a dictionary fails ? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-10 11:19 +0200
csiph-web